我有一个基本的摆动JTable,要求是当点击任何单元格时,整个行应该突出显示,并且单击的单元格应该与突出显示的行的其余部分颜色不同.
目前,我已将isRowSelectionAllowed视为true
我尝试使用自定义TableCellRenderer,如下所示:
public class CustomTableCellRenderer extends DefaultTableCellRenderer
{
public static final DefaultTableCellRenderer DEFAULT_RENDERER = new DefaultTableCellRenderer();
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
Component c = DEFAULT_RENDERER.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
if (isSelected) {
c.setBackground(Color.red);
}
else {
c.setForeground(Color.black);
c.setBackground(Color.white);
}
return c;
}
}
但这似乎不起作用(整行以红色突出显示).
我还尝试设置UIManager属性如下:
UIManager.put("Table.focusCellBackground",
new javax.swing.plaf.ColorUIResource (Color.red));
但这似乎也不起作用(尽管,当我尝试使用边框设置边框时)
UIManager.put("Table.focusCellHighlightBorder",
new BorderUIResource.LineBorderUIResource(Color.red));
效果很好)
你能否提出我可能需要做的建议?