我有一個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