JTree 忽略 LaF 覆盖

JTree ignoring LaF overrides

我最近一直在尝试将系统转换为统一的外观(在本例中为 Nimbus)。但是,某些组件需要更改默认值,我不想因为单个组件而只更改默认值。

我正在尝试覆盖 JTree 组件的行突出显示颜色。我已经设法完全关闭它,但这不是我想要的。

通过阅读这里的各种问题,我尝试使用:

UIDefaults overrides = new UIDefaults();
overrides.put("Tree.selectionBackground", new Color(200,200,200));
overrides.put("nimbusSelectionBackground", new Color(200,200,200));

modelTree = new ModelTree(treeModel);

modelTree.putClientProperty("Nimbus.Overrides", overrides);
modelTree.putClientProperty("Nimbus.Overrides.InheritDefaults",true);

其中 modelTree 扩展了标准 JTree。没有这些覆盖,外观完全是默认的,我知道在派生的 class.

中没有任何东西可以覆盖这些属性

问题是属性被忽略了,当 InheritDefaults 为 true 时,颜色与默认颜色保持不变,当它为 false 时,行突出显示被关闭。

我想要的是对行突出显示进行简单的重新着色。

提前致谢

编辑:

public static class treeTest extends JPanel {

    public treeTest() {
        super();

        try {
            UIManager.setLookAndFeel(NimbusLookAndFeel.class.getName());
        } catch (Exception e) {
            e.printStackTrace();
        }

        //UIManager.put("Tree.selectionForeground", Color.BLACK);

        UIDefaults overrides = new UIDefaults();

        overrides.put("Tree.selectionForeground", Color.BLACK);

        JTree defaultsTree = new JTree();
        JTree overiddenTree = new JTree();

        overiddenTree.putClientProperty("Nimbus.Overrides", overrides);
        overiddenTree.putClientProperty("Nimbus.Overrides.InheritDefaults", false);

        add(defaultsTree);
        add(overiddenTree);
    }
}

我希望这是一个可以接受的SSCCE(这是我第一次做)。 应该为 overiddenTree 组件覆盖选定行上的默认文本颜色,但没有。

取消注释覆盖默认值的行以执行相同的操作确实有效,但将为所有实例设置。

看看Nimbus Defaults (The Java™ Tutorials > Creating a GUI With JFC/Swing > Modifying the Look and Feel)

import java.awt.*;
import java.awt.image.*;
import javax.swing.*;
import javax.swing.tree.*;
import javax.swing.plaf.nimbus.*;

public class TreeCellBackgroundPainterTest {
  public JComponent makeUI() {
    UIDefaults d = new UIDefaults();
    AbstractRegionPainter rp = new AbstractRegionPainter() {
      @Override protected void doPaint(
          Graphics2D g, JComponent c, int width, int height, Object[] extendedCacheKeys) {
        g.setColor(Color.BLACK);
        g.fillRect(0, 0, width, height);
      }
      @Override protected final PaintContext getPaintContext() {
        return null;
      }
    };
    d.put("Tree:TreeCell[Enabled+Selected].backgroundPainter", rp);
    d.put("Tree:TreeCell[Focused+Selected].backgroundPainter", rp);

    JTree tree = new JTree();
    tree.putClientProperty("Nimbus.Overrides", d);
    tree.putClientProperty("Nimbus.Overrides.InheritDefaults", true);

    JPanel p = new JPanel(new GridLayout(1, 2, 2, 2));
    p.add(new JScrollPane(new JTree()));
    p.add(new JScrollPane(tree));
    return p;
  }
  public static void createAndShowGUI() {
    try {
      for (UIManager.LookAndFeelInfo laf : UIManager.getInstalledLookAndFeels()) {
        if ("Nimbus".equals(laf.getName())) {
          UIManager.setLookAndFeel(laf.getClassName());
        }
      }
    } catch (ClassNotFoundException | InstantiationException
           | IllegalAccessException | UnsupportedLookAndFeelException ex) {
      ex.printStackTrace();
    }
    JFrame f = new JFrame();
    f.getContentPane().add(new TreeCellBackgroundPainterTest().makeUI());
    f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    f.setSize(320, 240);
    f.setLocationRelativeTo(null);
    f.setVisible(true);
  }
  public static void main(String... args) {
    EventQueue.invokeLater(new Runnable() {
      @Override public void run() {
        createAndShowGUI();
      }
    });
  }
}