JTree Line Style and Nimbus
Asked Answered
S

3

7

I am using the Nimbus look and feel. According to this link, you should be able to achieve 3 different line styles with your JTree:

enter image description here

While using the following code:


theTree.putClientProperty("JTree.lineStyle", "Horizontal");

My JTree looks like this:

enter image description here

It has the "None" style and not the "Horizontal" style. Any idea why this might be? Does it have something to do with Nmbus? Do I need to call something special after setting that property?

Thanks

Submerged answered 18/2, 2011 at 15:10 Comment(2)
@Stack Yes I am. However my JTree is custom. It doesn't use the GUI editor.Submerged
I'm not sure then. Check out this post. The recommendation is to override setUI and updateUI.Czarist
M
6

I don't believe Nimbus supports the JTree.lineStyle property. Only the MetalLookAndFeel does.

Take a look at the source code for javax.swing.plaf.synth.SynthTreeUI (which is used by Nimbus) and MetalTreeUI (which is used by Metal).

Change to MetalLookAndFeel and see if it works.

Molotov answered 18/2, 2011 at 15:30 Comment(4)
Interesting. I did change it to Metal LaF and it did work. Kind of a let down that Nimbus doesnt support themSubmerged
I have tried to do this with Nimbus before and could not find a way that Nimbus supported it. I agree with dogbane's answer.Trencher
@eugener No I havent. Could you provide any links or details?Submerged
In short... you create your own tree cell renderer by inheriting from existing and overriding paint method to draw the line or putting default renderer JLabel component on the panel and adding separator to the bottom of the panel. Here is a link from Swing tutorial about tree cell renederers (scroll down a bit):download.oracle.com/javase/tutorial/uiswing/components/… Let me know if you need more info on that.Latoyia
S
5

Turns out you can get some of this effect by doing

NimbusLookAndFeel laf = new NimbusLookAndFeel();
UIManager.setLookAndFeel(laf);
nimbUID = laf.getDefaults();
nimbUID.put("Tree.drawHorizontalLines", true);
nimbUID.put("Tree.drawVerticalLines", true);

Not perfect, but close.

Selemas answered 28/4, 2012 at 18:0 Comment(0)
M
1

For anyone still interested in this:

The following snippet is working for me.

NewNimbusLookAndFeel laf = new NewNimbusLookAndFeel();

UIDefaults defs = laf.getDefaults();
defs.put("Tree.drawHorizontalLines", true);
defs.put("Tree.drawVerticalLines", true);
defs.put("Tree.linesStyle", "dashed");

try {
    UIManager.setLookAndFeel(laf);
} catch (UnsupportedLookAndFeelException e) {
    //Error handling code
}
Mammal answered 1/12, 2014 at 22:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.