Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Andrzejek on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

JTable.getSelectedRow() returning -1 when cell is selected

Status
Not open for further replies.

lesneskp

Programmer
Jul 3, 2002
19
US
Hello,

I imagine it's my renderer(??), but not sure why -- when I call JTable.getSelectedRow() (or for that matter, getSelectedColumn()), if a full row isn't selected, it will always return -1. But I need the row to be returned correctly even when just a cell is selected. Below is a clip of my cell renderer.

Advice is appreciated! Thanks!


public class CellLabel extends DefaultTableCellRenderer {
public static final Font FIXED_FONT = new Font("courier new", Font.PLAIN, 12);
public static final Font FIXED_FONT_BOLD = new Font("courier new", Font.BOLD, 12);
public static int STANDARD_COURIER_WIDTH = new JLabel().getFontMetrics(FIXED_FONT).charWidth('X');

private boolean mHighlightSelected;

public CellLabel(int horizontalAlignment, Font font, int avgLength, boolean highlight) {
this.setText("");
this.setFont(font);
this.setHorizontalAlignment(horizontalAlignment);
this.mHighlightSelected = highlight;
}

public CellLabel(int horizontalAlignment, Font font, int avgLength) {
this(horizontalAlignment, font, avgLength, true);
}

public Component getTableCellRendererComponent(
JTable jTable,
Object obj,
boolean isSelected,
boolean hasFocus,
int row,
int column) {

Object val = jTable.getModel().getValueAt(row, column);
this.setText((String)val);
if ( isSelected || hasFocus ) {
if ( this.getFont() == FIXED_FONT ) {
this.setFont(this.getFont().deriveFont(Font.BOLD));
}
if ( this.mHighlightSelected ) {
this.setBackground(new Color(204,204,255));
}
} else {
if ( this.getFont() == FIXED_FONT ) {
this.setFont(this.getFont().deriveFont(Font.PLAIN));
}
if ( this.mHighlightSelected ) {
this.setBackground(Color.white);
}
}
return this;
}
}
 
Your renderer won't be affecting jtable.getSelectedRow() or jtable.getSelectedColumn(), it only influences the display. Both these methods will return -1 if only a cell is selected.

try getting the Point that the pointer is currently at then calling jtable.columnAtPoint(mousePoint) or jtable.rowAtPoint(mousePoint).

If you're calling the code in a mouseListener the Point can be retrieved from the MouseEvent.getPoint(); Jeremy Nicholson, Director of a UK-based Java and Data Warehousing consultancy
 
Thanks. I'd rather not rely on finding points and whatnot, especially anything having to do with the mouse, since the keyboard might have been used instead. Not to mention that the table _should_ be able to give me this information. Many people have suggested that those methods do in fact work when a cell is selected, which is why I guessed that my renderer might be in the way... perhaps it is another part of my code... But in the end, I came up with a hack that seems to work very well:

I found that when a row is not selected (only a cell), I can use a ListSelectionListener (see below) to store the value of the last row number that was selected. It works, but is a complete kludge.

If you have any other input, I am still definitely interested in solving the problem the RIGHT way. Thanks.

table.getSelectionModel().addListSelectionListener(new javax.swing.event.ListSelectionListener() {
public void valueChanged(javax.swing.event.ListSelectionEvent e) {

// grab the row affected, as a temporary hack because when
// just a cell is selected, table.getSelectedRow() is
// returning -1 (not sure why)
// (the only case for which we use this number, both
// getFirstIndex() and getLastIndex() return the
// same value)
//
currentSelectedRow = e.getLastIndex();

}
});


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top