我想实现一个JTable组件的tablecellrenderer,它应该根据单元格数据显示不同的颜色。我知道了,但我不能更改选定单元格的颜色。我试着这样做:
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int rowIndex, int vColIndex)
{
if (isSelected) {
this.setBackground((Color)UIManager.get("Table.selectionBackground"));
this.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
} else {
this.setForeground((Color)UIManager.get("Table.foreground"));
this.setBackground((Color)UIManager.get("Table.background"));
this.setBorder(BorderFactory.createEmptyBorder());
}
...
}但它不起作用:S ..我看不到问题,因为当我单击一个单元格时,JTable没有显示任何不同的内容。
发布于 2011-02-24 10:16:20
我想实现一个JTable组件的tablecellrenderer,它应该根据单元格数据显示不同的颜色
您发布的代码不能做到这一点。基本上,您的代码所做的就是复制渲染器的默认行为
您可能会发现Table Row Rendering方法更容易实现。
发布于 2011-03-03 03:03:34
假设您使用JLabel作为组件的基础,除非您还将opaque设置为true,否则设置背景将不起作用。JLabels默认设置为不透明,因此不绘制背景。
https://stackoverflow.com/questions/5098055
复制相似问题