我有一个基本的摇摆 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));
效果很好)
您能否提出任何建议,我可能需要做什么?