JTable TableCellRenderer背景与NimbusLookAndFeel颜色问题
我正在使用 NimbusLookAndFeel。通过这种外观和感觉,JTable 的单元格背景可以是白色和浅灰色(这取决于行号)。 现在,我正在编写一些实现 TableCellRenderer 的自定义单元格渲染器。我需要根据 JTable 中单元格的位置设置这些渲染器的背景。
public class MyCellRenderer extends JLabel implements TableCellRenderer{
@Override
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
Color bgColor = //need to retrieve the right cell background color
setBackground(bgColor);
return this;
}
}
我怎样才能得到这样的颜色值?
I'm using NimbusLookAndFeel. With this look and feel JTable's cell background are alternatively white and light grey (it depends on the row number).
Now, I'm writing some custom cell renderer implementing TableCellRenderer. I need to set the background of these renderers according to the position of the cell in the JTable.
public class MyCellRenderer extends JLabel implements TableCellRenderer{
@Override
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
Color bgColor = //need to retrieve the right cell background color
setBackground(bgColor);
return this;
}
}
How can I get such a Color value?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从技术上讲,您可以通过 UIManager 访问颜色。
实际上,我不建议从头开始编写渲染器 - 需要考虑许多细节才能使其正确。这些细节是在 SwingX 中处理的(对我有偏见:-)
希望你自己做逻辑;-)。这是一个工作片段(假设您想按行而不是按列着色,但更改它......微不足道):
Technically, you can access the color via the UIManager
Practically, I would not recommend to write renderers from scratch - there are many details to consider to get it right. Those details are handled f.i. in SwingX (biased me :-)
Expected you to do the logic yourself ;-). Here's a working snippet (assuming you want to color by row not by column, but changing that would be ... trivial):
这应该可以完美地工作:
This should work perfectly :