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

user489041
  • 27,916
  • 55
  • 135
  • 204
  • @Stack Yes I am. However my JTree is custom. It doesn't use the GUI editor. – user489041 Feb 18 '11 at 15:19
  • I'm not sure then. Check out this [post](http://stackoverflow.com/questions/1347607/netbeans-jtree-linestyle). The recommendation is to override setUI and updateUI. – Bala R Feb 18 '11 at 15:23

3 Answers3

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.

dogbane
  • 266,786
  • 75
  • 396
  • 414
  • Interesting. I did change it to Metal LaF and it did work. Kind of a let down that Nimbus doesnt support them – user489041 Feb 18 '11 at 15:31
  • 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. – jzd Feb 18 '11 at 15:33
  • @eugener No I havent. Could you provide any links or details? – user489041 Feb 21 '11 at 14:28
  • 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):http://download.oracle.com/javase/tutorial/uiswing/components/tree.html#display Let me know if you need more info on that. – Eugene Ryzhikov Feb 21 '11 at 22:06
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.

user1359010
  • 243
  • 3
  • 11
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
}