我有一个JTable
显示Java Swing应用程序中的一些记录。用户只能使用整行,而不是单个单元。有什么办法可以专注于JTable
的整行?默认设置是只关注用户点击的单元格。我为焦点使用了不同的颜色,所以如果只有一个细胞聚焦而不是整行,它看起来不太好。如何关注一整行JTable?
UPDATE:这是我目前我的自定义TableCellRenderer
的代码,问题是,当行具有焦点,并绘有“焦点访谈”的颜色,然后JTable
失去焦点,只有细胞那焦点重新用“选定”颜色重新绘制,但选定行中的所有单元格都应使用“选定”颜色重新绘制。
@Override
public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus,
int row, int column) {
// Has any cell in this row focus?
if(table.getSelectedRow() == row && table.hasFocus()) {
hasFocus = true;
}
if (hasFocus) {
this.setBackground(CustomColors.focusedColor);
} else if (isSelected) {
this.setBackground(CustomColors.selectedColor);
} else {
// alternate the color of every second row
if(row % 2 == 0) {
this.setBackground(Color.WHITE);
} else {
this.setBackground(CustomColors.grayBg);
}
}
setValue(value);
return this;
}
是否足以使用自定义cellrenderer来显示行中的所有单元格,因为它们将具有焦点? – Howard 2011-03-31 16:39:15
@霍华德:是的,如果你有建议,那就足够了。 – Jonas 2011-03-31 16:48:05
另请参阅[表格行渲染](http://tips4java.wordpress.com/2010/01/24/table-row-rendering/)。 – trashgod 2011-04-01 02:57:40