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;
}
}
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;
}
}