JTree忽略LaF覆盖 [英] JTree ignoring LaF overrides

查看:101
本文介绍了JTree忽略LaF覆盖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have recently been attempting to convert a system to a unified look and feel (In this case Nimbus). However certain components require alteration from the defaults and I don't want to just change the defaults because of a single component.

我试图覆盖JTree组件的行突出显示颜色.我设法将其完全关闭,但这不是我想要的.

I am trying to override the row highlight colour for a JTree component. I have managed to switch it off entirely but this is not what I am after.

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

From reading the various questions on here I have tried using:

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的位置.没有这些覆盖,外观将完全是默认外观,并且我知道派生类中没有覆盖这些属性的东西.

Where modelTree extends the standard JTree. Without these overrides the look is entirely default and I know that there is nothing overriding these properties in the derrived class.

问题在于,当InheritDefaults为true时,属性将被忽略,颜色与默认值相同,为false时将关闭行高亮显示.

The problem is that the properties are being ignored, the colour stays the same as default while InheritDefaults is true and the row highlight is switched off when it is false.

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

What I am after is a simple recolouring of the row highlight.

预先感谢

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(这是我第一次创建SSCCE). 应该覆盖overiddenTree组件的所选行上的默认文本颜色,但不能.

I hope this is an acceptable SSCCE (It's the first time I've made one). Should override the default text colour on the selected line for the overiddenTree component but doesn't.

取消注释覆盖默认行以执行相同操作的注释确实可行,但会为所有实例设置.

Uncommenting the line that overwrites the default to do the same does work but will set for all instances.

推荐答案

看看 Nimbus默认值(Java™教程>使用JFC/Swing创建GUI>修改外观)

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

这篇关于JTree忽略LaF覆盖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆