为 Nimbus 外观设计简单的单元渲染器
我有一个简单的单元格渲染器,它由几个 JLabel
组成(渲染器本身扩展了 JPanel
),我试图让它在Nimbus 的外观和感觉。基本上发生的情况是,在较亮行中(因为 Nimbus 有替代行着色),我的特定单元格渲染器使用表格背景颜色(比两者都暗得多)较浅和较深的行颜色)。在我的渲染器中,我这样做:
if (isSelected) {
setBackground(table.getSelectionBackground);
}
else {
setBackground(table.getBackground);
}
如果我注释掉整个代码块,那么我的所有行都将采用较暗的 row 颜色(不是表格背景,但也不采用替代颜色)。我不确定我是否明白发生了什么!上面的代码片段是如何产生具有不同背景颜色的单元格的? table.getBackground
颜色在我的方法调用之间是否发生变化?
我尝试使用这段代码:
Color alternateColor = sun.swing.DefaultLookup.getColor(
peer,
peer.getUI,
"Table.alternateRowColor");
if (alternateColor != null && row % 2 == 0)
setBackground(alternateColor);
它位于 DefaultTableCellRenderer
类中。而且似乎根本没有任何影响。有人有与 Nimbus 一起使用的自定义单元渲染器吗?
编辑:如果有人感兴趣,结果证明这是 Scala 表格单元格渲染器的问题,因为我实际上使用的是 Scala,而不是 Java。下面接受的答案在 Java 程序中运行得很好。 此处提交了单独的问题。
I have a simple-ish cell renderer which is composed of a few JLabel
s (the renderer itself extends JPanel
) and I'm trying to get it to render sensibly in the Nimbus look and feel. Basically what is happening is that in the lighter rows (as Nimbus has alternate row coloring), my specific cell renderer is using the table background color (which is much darker than both lighter and the darker row colors). In my renderer I do:
if (isSelected) {
setBackground(table.getSelectionBackground);
}
else {
setBackground(table.getBackground);
}
If I comment this whole block of code out then then all my rows are in the darker row color (not the table background, but not in alternate colors either). I'm not sure I even understand what can be going on! How is the above snippet of code producing cells with different background colors at all? Is the table.getBackground
color changing between invocations of my method?
I've tried using this snippet of code:
Color alternateColor = sun.swing.DefaultLookup.getColor(
peer,
peer.getUI,
"Table.alternateRowColor");
if (alternateColor != null && row % 2 == 0)
setBackground(alternateColor);
Which is in the DefaultTableCellRenderer
class. And it doesn't seem to have any affect at all. Has anyone got custom cell renderers working with Nimbus?
EDIT: If anyone is interested, this turned out to be a problem with Scala table cell renderers, as I was actually using Scala, not Java. The accepted answer below works just fine in a Java program. Separate question filed here.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的第一段代码如果没问题的话。我认为你必须使用 UIManager.getColor("Table.alternateRowColor") 来替代行,否则使用 table.getBackground() 。对于选定的行,请使用 table.getSelectionBackground()。因此,您的代码可能看起来像“
不要忘记确保您的面板是不透明的并且标签是透明的”。
这是 Nimbus UI 默认值的一个很好的链接:
http://www.duncanjauncey.com/java/ui /uimanager/UIDefaults_Java1.6.0_11_Windows_2000_5.0_Nimbus.html
Your first piece of code if fine.I think you have to use
UIManager.getColor("Table.alternateRowColor")
for alternate rows and table.getBackground() otherwise. For selected row use table.getSelectionBackground(). So your code might look likeDon't forget to make sure that your panel is opaque and the labels are transparent.
Here is a good link to Nimbus UI defaults:
http://www.duncanjauncey.com/java/ui/uimanager/UIDefaults_Java1.6.0_11_Windows_2000_5.0_Nimbus.html