【问题标题】:Nimbus and alternate row colorsNimbus 和备用行颜色
【发布时间】:2012-10-12 01:47:17
【问题描述】:

我不明白 Nimbus 中交替行着色的工作原理。看起来简直是疯了!!!我想在这里澄清一下。

为了演示,假设我们想要一个交替红色和粉红色行的 JTable(我不在乎第一个是哪种颜色)。

不重新定义自定义 cellRenderers 来执行他们自己的“模 2”事情,并且不覆盖 JTable 中的任何方法,我想列出启动应用程序和使用自定义替代获取 JTable 之间的强制性步骤行颜色仅使用 Nimbus 属性

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

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

这是 JDK6 代码。 有人能告诉我这里出了什么问题吗?


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

公共类 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);
}

}

【问题讨论】:

  • 没有任何问题。代码对我来说很好,除了 jdk6 的第一行和第三行是粉红色的,而 jdk7 只有第二行是。
  • 不,没有什么疯狂的,必须接受,所有 Randerer 都可以不受限制地工作(对于在 API 中实现 Renderer 的工作),因为 UIManager 必须覆盖 Nimbus Default 中的 proeprs Key(s),当然有时颜色,Painter 某处,问题可能仅与 XxxUIResouces 相关(基本上与问题 1-8 无关),每个键都已正确描述...,顺便说一句,问题中的形式,尤其是第 1-8 点是 vWorker 的重要补充.com
  • @DenisTulskiy 没有在红色和粉红色之间交替的行实际上是错误的。要么是我,要么是 Nimbus,但我们中的一个人错了!
  • 在这种情况下,我通常不得不深入研究 Nimbus 源代码,看看它是如何绘制东西的。我建议你也参加那段很棒的旅程:)
  • 没有在红色和粉色之间交替的行实际上是错误的在你的问题中添加这句话,水晶球在星期一早上是出了名的不可靠;-)

标签: java swing jtable render nimbus


【解决方案1】:

貌似是几个bug的干扰……

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

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

对 Metal 和 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;
    }
}

它的逻辑是错误的:只有当 table 的背景是 colorUIResource 时才采用替代颜色,这是一个相当微弱的区别。无论如何,它引导我们进行下一次尝试:

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

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

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

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

编辑(在 cmets 中被要求)

JXTable 试图完全回避这个问题 - 它的条带化方法是从 HighlighterFactory 检索的 Highlighter。需要通过从lookAndFeelDefaults 中删除alternateRowColor 属性并使用新键“UIColorHighlighter.stripingBackground”添加它来使用Nimbus

【讨论】:

  • +1 恭喜您成功了 :)。 1个问题为什么这不起作用,因为我一开始尝试过但没有运气,如您所见:UIManager.getDefaults().put("Table:\"Table.cellRenderer\".background", Color.RED);
  • @David Kroukamp 在这里查看我的帖子(基本内容 +1)
  • 如果你使用SwingX 和它的荧光笔,你会免费获得所有这些黑客攻击吗?我从来没有遇到过创建交替行颜色的任何问题,但我不使用标准的JTable
  • @Robin 我看到只有在 UIManager 中覆盖 Key 的问题,在这个答案中描述得很好
  • @mKorbel 描述得非常好,但需要进行一定程度的调试才能发现应该是一个几乎微不足道的用例。设计表格时,具有交替行颜色的表格是非常基本的要求
【解决方案2】:

使用 Nimbus 属性(+1 到 @Kleopatra 证明我错了 :() 您可以通过

设置交替行颜色
UIManager.put("Table.alternateRowColor", Color.PINK);
UIManager.getLookAndFeelDefaults().put("Table:\"Table.cellRenderer\".background", Color.RED);

替代方式:

扩展 JTable 并覆盖 prepareRenderer(TableCellRenderer renderer, int row, int column) 以便为单元格绘制所需的颜色(REDPINK)。

这是一个简短的示例,希望对您有所帮助。

额外功能: 它还覆盖paintComponent(..),它将调用paintEmptyRows(Graphics g),它将为JScrollPane视口的整个高度和宽度绘制行,但这仅适用于setFillsViewPortHeightMyTable上设置为true

import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Rectangle;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.WindowConstants;
import javax.swing.plaf.nimbus.NimbusLookAndFeel;
import javax.swing.table.TableCellRenderer;

public class JTableAlternateRowColors {

    public JTableAlternateRowColors() {
        initComponents();
    }

    public static void main(String[] args) {

        try {
            UIManager.setLookAndFeel(new NimbusLookAndFeel());
        } catch (UnsupportedLookAndFeelException e) {
            e.printStackTrace();
        }

        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new JTableAlternateRowColors();
            }
        });
    }

    private void initComponents() {

        final JFrame jFrame = new JFrame("Nimbus alternate row coloring");

        MyTable table = new MyTable(new String[][]{
                    {"one", "two", "three"},
                    {"one", "two", "three"},
                    {"one", "two", "three"}
                }, new String[]{"col1", "col2", "col3"});

        table.setFillsViewportHeight(true);//will fill the empty spaces too if any

        table.setPreferredScrollableViewportSize(table.getPreferredSize());

        JScrollPane jScrollPane = new JScrollPane(table);

        jFrame.getContentPane().add(jScrollPane);
        jFrame.pack();
        jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        jFrame.setVisible(true);
    }
}

class MyTable extends JTable {

    public MyTable(String[][] data, String[] fields) {
        super(data, fields);
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        if (getFillsViewportHeight()) {
            paintEmptyRows(g);
        }
    }

    /**
     * Paints the backgrounds of the implied empty rows when the table model is
     * insufficient to fill all the visible area available to us. We don't
     * involve cell renderers, because we have no data.
     */
    protected void paintEmptyRows(Graphics g) {
        final int rowCount = getRowCount();
        final Rectangle clip = g.getClipBounds();
        if (rowCount * rowHeight < clip.height) {
            for (int i = rowCount; i <= clip.height / rowHeight; ++i) {
                g.setColor(colorForRow(i));
                g.fillRect(clip.x, i * rowHeight, clip.width, rowHeight);
            }
        }
    }

    /**
     * Returns the appropriate background color for the given row.
     */
    protected Color colorForRow(int row) {
        return (row % 2 == 0) ? Color.RED : Color.PINK;
    }

    /**
     * Shades alternate rows in different colors.
     */
    @Override
    public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
        Component c = super.prepareRenderer(renderer, row, column);
        if (isCellSelected(row, column) == false) {
            c.setBackground(colorForRow(row));
            c.setForeground(UIManager.getColor("Table.foreground"));
        } else {
            c.setBackground(UIManager.getColor("Table.selectionBackground"));
            c.setForeground(UIManager.getColor("Table.selectionForeground"));
        }
        return c;
    }
}

参考资料:

【讨论】:

  • +1 prepateRenderer,并添加关于 convertXxxXxx 进行排序或过滤的通知
  • FillViewPortHeight 的东西很漂亮也很精致(我喜欢),但不是这里的主题所以我不会评论它......
  • @DavidKroukamp 第 3 方确实扩展了 JTable,但是如何扩展嵌入在 JComponent 层次结构中的 JTable?如何扩展final类?你如何处理混淆代码?您如何处理插件提供的 JTables?我知道如何交替颜色。我已经做到了。很抱歉,我无法接受您的回答但我只能对仅适用于 Nimbus 属性的解决方案感到满意。如果这不可能,那么 Nimbus 就是垃圾!就这么简单!否则,为什么要提供属性“Table.alternateRowColor”????
  • @Pigelvy 如果你需要通过大喊来发泄你的挫败感,请向 Oracle 的方向这样做:Nimbus IS buggy!
  • @David Kroukamp 是否有车,开发在第一季度/第二季度结束,这个 L&F 是关于颜色主题而不是关于装满单独闪闪发光的鱼的水族馆,有一些用于闪闪发光的配色方案的 xml 文件,然后不需要 Nimbus#uinstallUI 用于一些带有 null 值的错误键,默认情况下允许并且 (short_cuts) 用于渲染器概念
猜你喜欢
  • 2014-08-02
  • 2011-12-17
  • 2015-01-06
  • 2011-11-02
  • 1970-01-01
  • 2011-09-22
  • 2018-06-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多