为Nimbus外观设计简单的单元渲染器 [英] Designing simple cell renderer for Nimbus look and feel

查看:159
本文介绍了为Nimbus外观设计简单的单元渲染器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的单元格渲染器,它由一些 JLabel 组成(渲染器本身扩展 JPanel )我试图让它在Nimbus的外观和感觉中合理地呈现。基本上发生的事情是在更轻的行中(因为Nimbus有交替行着色),我的特定单元格渲染器使用表格背景颜色(比两者都暗很多)更亮和更深的颜色)。在我的渲染器中,我这样做:

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);
}

如果我对整个代码块进行评论,那么我的所有行都在较暗的颜色(不是表格背景,但也不是替代颜色)。我不确定我是否理解可能发生的事情!上面的代码片段如何产生具有不同背景颜色的单元格? 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?

我试过使用这段代码:

Color alternateColor = sun.swing.DefaultLookup.getColor(
                         peer, 
                         peer.getUI, 
                         "Table.alternateRowColor");
if (alternateColor != null && row % 2 == 0)
    setBackground(alternateColor);

这是在 DefaultTableCellRenderer 类中。它似乎没有任何影响。有没有人有使用Nimbus的自定义单元格渲染器?

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?

编辑:如果有人有兴趣,这对来说是一个问题Scala 表格单元格渲染器,因为我实际上使用的是Scala,而不是Java。下面接受的答案在Java程序中运行得很好。单独的问题提交 此处

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.

推荐答案

你的第一段代码如果罚款。我认为你必须使用 UIManager.getColor(表。 alternateRowColor)表示备用行,否则表示table.getBackground()。对于选定的行使用table.getSelectionBackground()。因此,您的代码可能看起来像

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.

这是Nimbus UI默认值的一个很好的链接:
http://www.duncanjauncey.com/java/ui/uimanager/UIDefaults_Java1.6.0_11_Windows_2000_5.0_Nimbus.html

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

这篇关于为Nimbus外观设计简单的单元渲染器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆