5

我不明白在 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);
    }
}

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

}

4

2 回答 2

11

貌似是几个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 默认值,并应用(在设置 LAF 之后):

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

编辑(正如评论中所要求的那样)

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

于 2012-10-22T13:29:15.103 回答
3

使用 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)以便为单元格绘制所需的颜色(红色粉红色)。

这是一个简短的例子,我希望它有所帮助。

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

在此处输入图像描述

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

参考:

于 2012-10-22T11:21:11.323 回答