Nimbus和备用行颜色

Pig*_*lvy 5 java swing render jtable nimbus

我不明白Nimbus的替代行着色是如何工作的.看起来真的很疯狂!我想在这里澄清一切.

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

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

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

  1. 安装Nimbus PLAF
  2. 自定义"Table.background"nimbus属性
  3. 自定义"Table.alternateRowColor"nimbus属性
  4. 使用简单的数据/标头创建JTable
  5. 将jTable包装在JScrollPane中并将其添加到JFrame
  6. 显示JFrame

这里的源代码:


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);
    }
}
Run Code Online (Sandbox Code Playgroud)

这是JDK6代码.谁能告诉我这里出错了?


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

public class JTableAlternateRowColors实现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);
}
Run Code Online (Sandbox Code Playgroud)

}

kle*_*tra 10

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

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

UIManager.put("Table.background", Color.RED);
UIManager.put("Table.alternateRowColor", Color.PINK);
Run Code Online (Sandbox Code Playgroud)

对金属和Nimbus都不起作用

  • 金属:没有条纹,桌子都是红色的
  • 在Nimbus中:条纹白色/粉红色,即表格背景被忽略

第一个的基本原因可以在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;
    }
}
Run Code Online (Sandbox Code Playgroud)

它的逻辑是弯曲的:只有当桌子的背景是​​colorUIResource 时才采用替代颜色,这是一个相当弱的区别.无论如何,它引导我们下一次尝试:

UIManager.put("Table.background", new ColorUIResource(Color.RED));
UIManager.put("Table.alternateRowColor", Color.PINK);
Run Code Online (Sandbox Code Playgroud)

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

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

UIManager.getLookAndFeelDefaults().put("Table:\"Table.cellRenderer\".background", 
    new ColorUIResource(Color.RED));
Run Code Online (Sandbox Code Playgroud)

编辑(如评论中所述)

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

  • @mKorbel它的描述非常好,但需要一定的调试才能发现一些应该是一个几乎无足轻重的用例.在设计表格时,具有交替行颜色的表格是一个非常基本的要求 (2认同)