• Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Micha Jones
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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;
}
}
 
Michael Crutcher
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It sounds like you're interested in the selection model for your JTable, not the custom renderer you made.
Did you call:

This code will do the same thing:

It sounds like row selection is set to not allowed.
Hope this helps some.
Michael Crutcher
 
Micha Jones
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried the method calls you suggest, and a number of combinations with allowing and disallowing selection of rows, columns, cells, etc. Nothing worked.
As a hack, 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();

}
});
 
Chantal Ackermann
Ranch Hand
Posts: 508
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is no new input, just wanted to say: I find your solution quite clean, I don't think it's a "kludge". for what other reason would this event provide these methods that allow you to retrieve the selection index?
 
Micha Jones
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the thought. I think it is a hack because JTable's getSelectedRow() should be all I need; I shouldn't have had to do that. Furthermore, the getFirstIndex() and getLastIndex() in most cases (when entire row(s) are selected) give you a range of rows that may have changed in the table, NOT the range of rows currently selected. I found that when a single cell is selected, both of those methods pass back just that cell's row number, which was lucky for me, but I don't think this part of the API was intended for such purposes.
Thanks again.
Cheers!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!