Nimbus和备用行颜色 [英] Nimbus and alternate row colors

查看:111
本文介绍了Nimbus和备用行颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白Nimbus中替代行着色的工作原理。看起来真的很疯狂!我想在这里澄清一下。

I don't understand how alternate row coloring works in Nimbus. It seems just crazy!!! I would like to clear things up here.

对于演示,让我们说我们想要一个交替红色和粉红色行的JTable (和我不关心哪种颜色是第一种颜色。)

For the demonstration, let's say that we want a JTable that alternate Red and Pink rows (and I don't care which color is the first one).

没有重新定义自定义的cellRenderers 执行他们自己的模2的事情,并且如果没有覆盖JTable中的任何方法,我想列出启动一个应用程序和使用自定义备用行颜色获取JTable之间的必要步骤仅使用Nimbus属性

Without redefining custom cellRenderers that perform their own "modulo 2" thing, and without overriding any method from JTable, I want to list the mandatory steps between starting one's application and getting a JTable with custom alternate row colors using Nimbus properties only.

以下是我希望遵循的步骤:

Here are the steps I expected to follow:


  1. 安装Nimbus PLAF

  2. 自定义Table.backgroundnimbus属性

  3. 自定义Table.alternateRowColornimbus属性

  4. 使用简单数据/标题创建JTable

  5. 将jTable包装在JScrollPane中并将其添加到JFrame

  6. 显示JFrame

  1. Install the Nimbus PLAF
  2. Customize the "Table.background" nimbus property
  3. Customize the "Table.alternateRowColor" nimbus property
  4. Create a JTable with simple data/header
  5. Wrap the jTable in a JScrollPane and add it to the JFrame
  6. Show the JFrame

这里是源鳕鱼e:

public class JTableAlternateRowColors implements Runnable {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new JTableAlternateRowColors());
    }

    @Override
    public void run() {
        try {
            UIManager.setLookAndFeel(new NimbusLookAndFeel());
        } catch (UnsupportedLookAndFeelException e) {
            e.printStackTrace();
        }

        UIManager.getDefaults().put("Table.background", Color.RED);
        UIManager.getDefaults().put("Table.alternateRowColor", Color.PINK);

        final JFrame jFrame = new JFrame("Nimbus alternate row coloring");
        jFrame.getContentPane().add(new JScrollPane(new JTable(new String[][] {
                {"one","two","three"},
                {"one","two","three"},
                {"one","two","three"}
        }, new String[]{"col1", "col2", "col3"}
        )));
        jFrame.setSize(400, 300);
        jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        jFrame.setVisible(true);
    }
}

这是JDK6代码。
有人可以告诉我这里出错吗?

This is JDK6 code. Can somebody tell me goes wrong here?

根据@ kleopatra的评论以及整个社区的贡献这里是使用Nimbus属性获得备用行着色的方法

As per @kleopatra's comment and the contribution of the whole community here's a/the way to get alternate row coloring using only Nimbus properties

公共类JTableAlternateRowColors实现Runnable {

public class JTableAlternateRowColors implements Runnable {

public static void main(String[] args) {
    SwingUtilities.invokeLater(new JTableAlternateRowColors());
}

@Override
public void run() {
    try {
        UIManager.setLookAndFeel(new NimbusLookAndFeel());
    } catch (UnsupportedLookAndFeelException e) {
        e.printStackTrace();
    }

    UIManager.put("Table.background", new ColorUIResource(Color.RED));
    UIManager.put("Table.alternateRowColor", Color.PINK);
    UIManager.getLookAndFeelDefaults().put("Table:\"Table.cellRenderer\".background", new ColorUIResource(Color.RED));

    final JFrame jFrame = new JFrame("Nimbus alternate row coloring");
    final JTable jTable = new JTable(new String[][]{
            {"one", "two", "three"},
            {"one", "two", "three"},
            {"one", "two", "three"}
    }, new String[]{"col1", "col2", "col3"});
    jTable.setFillsViewportHeight(true);
    jFrame.getContentPane().add(new JScrollPane(jTable));
    jFrame.setSize(400, 300);
    jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    jFrame.setVisible(true);
}

}

推荐答案

看起来像几个bug的干扰......

Looks like the interference of several bugs ...

用于更改默认表格背景和默认条带化,预期(不仅仅是)你的,我的也是)UIManager的配置(对于所有尊重alternateRow属性的LAF都相同)将是:

For changing both default table background and default striping, the expected (not only yours, mine as well) configuration of the UIManager (same for all LAFs which respect the alternateRow property) would be:

UIManager.put("Table.background", Color.RED);
UIManager.put("Table.alternateRowColor", Color.PINK);

对Metal和Nimbus都不起作用

Doesn't work, neither for Metal nor for Nimbus


  • :没有条纹,表格全是红色

  • 在Nimbus中:条纹白色/粉红色,即表格背景被忽略

第一个的基本原因可以在DefaultTableCellRenderer中找到:

Underlying reason for the first can be found in DefaultTableCellRenderer:

Color background = unselectedBackground != null
                        ? unselectedBackground
                        : table.getBackground();
if (background == null || background instanceof javax.swing.plaf.UIResource) {
    Color alternateColor = DefaultLookup.getColor(this, ui, "Table.alternateRowColor");
    if (alternateColor != null && row % 2 != 0) {
        background = alternateColor;
    }
}

它的逻辑是弯曲的:只采取替代颜色如果 table 的背景是colorUIResource,那是一个相当弱的区别。无论如何,它引导我们下一次尝试:

It's logic is crooked: the alternate color is only taken if the table's background is a colorUIResource, a rather weak distinction. Anyway, it leads us to next try:

UIManager.put("Table.background", new ColorUIResource(Color.RED));
UIManager.put("Table.alternateRowColor", Color.PINK);

这看起来很好(除了复选框渲染器的典型问题,但这是另一个错误故事; - 对于金属而言,Nimbus仍然没有运气。

This looks fine (except the typical issue with a checkbox renderer, but that's yet another bug story ;-) for metal, still no luck for Nimbus.

下一步是查找 Nimbus默认值可能是相关的,并且适用(在设置LAF之后):

Next step is look up Nimbus defaults which might be related, and apply (after! setting the LAF):

UIManager.getLookAndFeelDefaults().put("Table:\"Table.cellRenderer\".background", 
    new ColorUIResource(Color.RED));

编辑(正如评论中提到的那样)

Edit (as it was asked in the comments)

JXTable试图完全解决问题 - 它的条带化手段是从HighlighterFactory中检索到的Highlighter。需要通过从lookAndFeelDefaults中删除alternateRowColor属性来弄脏Nimbus,并使用新键UIColorHighlighter.stripingBackground添加它

JXTable tries to side-step the problem entirely - its means for striping is a Highlighter retrieved from the HighlighterFactory. Needs to go dirty with Nimbus by removing the alternateRowColor property from the lookAndFeelDefaults and add it with a new key "UIColorHighlighter.stripingBackground"

这篇关于Nimbus和备用行颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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