为 Nimbus 外观设计简单的单元渲染器

发布于 2024-08-02 23:12:09 字数 1166 浏览 7 评论 0原文

我有一个简单的单元格渲染器,它由几个 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 JLabels (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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一袭白衣梦中忆 2024-08-09 23:12:09

你的第一段代码如果没问题的话。我认为你必须使用 UIManager.getColor("Table.alternateRowColor") 来替代行,否则使用 table.getBackground() 。对于选定的行,请使用 table.getSelectionBackground()。因此,您的代码可能看起来像“

if (isSelected) {
    setBackground(table.getSelectionBackground());
}
else {
    if ( row % 2 == 0 ) {
       setBackground(UIManager.getColor("Table.alternateRowColor"));
    } else { 
       setBackground(table.getBackground());
    }
}

不要忘记确保您的面板是不透明的并且标签是透明的”。

这是 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 like

if (isSelected) {
    setBackground(table.getSelectionBackground());
}
else {
    if ( row % 2 == 0 ) {
       setBackground(UIManager.getColor("Table.alternateRowColor"));
    } else { 
       setBackground(table.getBackground());
    }
}

Don'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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文