Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 785
  • Last Modified:

How to change linestyle in JTree

How can I change the linestyle from dotted to solid in the swing component JTree ?
0
eeks2
Asked:
eeks2
  • 6
  • 3
1 Solution
 
vladi21Commented:
http://java.sun.com/docs/books/tutorial/uiswing/components/tree.html
By default, the Java Look & Feel draws no lines between nodes. By setting the JTree.lineStyle client property of a tree, you can specify a different convention. For example, to request that the Java Look & Feel use horizontal lines to group nodes (as shown in the next figure), use the following code:

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


To specify that the Java Look & Feel should draw lines detailing the relationships between nodes (as shown in the next figure), use this code:
tree.putClientProperty("JTree.lineStyle", "Angled");

---
also look at Swing sources and examples

0
 
vladi21Commented:
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
eeks2Author Commented:
I know the examples, and I tried it, but on my machine this code has no effect. Lines are always dotted. Even if the code would work, I have only the choice between "None","Horizontal" and "Angled" but this is not what I want, because I want "solid angled" lines.
0
 
vladi21Commented:
Answer : you have only 3 choices,
but You can implement custom UI, i.e. you can extends javax\swing\plaf\metal\MetalTreeUI.java
0
 
eeks2Author Commented:
When I exchange JTree with MetalTreeUI then I get a lot of errormessages, so I have to reconfigure my whole program. I dont know the difference between JTree and MetalTreeUI, but they seem to use different methods.
0
 
vladi21Commented:
post ur code
0
 
vladi21Commented:
U must do something like this:

create class

class MyTreeUI extends MetalTreeUI {//or  BasicTreeU

ovveride 2 methods of BasicTreeU - look at sources

protected void paintVerticalPartOfLeg()
protected void paintHorizontalPartOfLeg()

}


JTree myTree=new JTree();

myTree.setUI(new MyTreeUI());
0
 
eeks2Author Commented:
Ok, this basically works. Interestingly I get only the paintHorizontalPartOfLeg event. I filled in following lines.
g.drawLine((int)bounds.getX()-12,(int)bounds.getY(),(int)bounds.getX()-12,(int)bounds.getY()+(int)bounds.getHeight()/2); //vertical part
g.drawLine((int)bounds.getX()-12,(int)bounds.getY()+bounds.getHeight()/2,(int)bounds.getX()-4,(int)bounds.getY()+(int)bounds.getHeight()/2); //horizontal part
Using this code I get solid lines.
Thank You Vladi for suggesting me hints to solve this question.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 6
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now