JTree ignoring LaF overrides
Asked Answered
E

1

1

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.

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

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.

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.

Thanks in advance

EDIT:

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

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.

Engender answered 24/2, 2015 at 8:40 Comment(2)
for better help sooner post an SSCCE/MCVE, short, runnable, compilable,Alcus
Ahh ofc, thanks. I'm new to all this so I shall try to create one asapEngender
D
2

Take a look at 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();
      }
    });
  }
}
Disbelief answered 24/2, 2015 at 12:49 Comment(1)
That's perfect thanks!! It does exactly what I need. I'm going to have to look into Painters as I'm unfamiliar with them.Engender

© 2022 - 2024 — McMap. All rights reserved.