JTree忽略了LaF覆盖

时间:2015-02-24 08:40:10

标签: java swing nimbus uimanager uidefaults

我最近一直试图将系统转换为统一的外观(在本例中为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。如果没有这些覆盖,那么外观完全是默认的,我知道在这个类中,没有什么能覆盖这些属性。

问题是属性被忽略,当InheritDefaults为true时颜色与默认值保持相同,并且当false为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组件的选定行上的默认文本颜色,但不是。

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

1 个答案:

答案 0 :(得分:2)

查看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();
      }
    });
  }
}