Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to create JList of JTrees?

 
Volodymyr Lysenko
Ranch Hand
Posts: 511
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!

I want to have many JTrees displayed one below another vertically.

I decided to use JList as vertical container for JTree.
The problem is after I added JTree to DefaultListModel and created JList with that model
I have toString of JTree displayed as item in JList instead of JTree itself . This is displayed :
javax.swing.JTree[,0,0,0x0,invalid,alignmentX=0.0,alignmentY=0.0,border=,flags=16777576,maximumSize=,minimumSize=,preferredSize=,editable=false,invokesStopCellEditing=false,largeModel=false,rootVisible=true,rowHeight=16,scrollsOnExpand=true,showsRootHandles=false,toggleClickCount=2,visibleRowCount=20]

How to tell JList to display JTree component instead of text of JTree.toString method?

Thank you!
 
Maneesh Godbole
Saloon Keeper
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In a nut shell, you need to provide your own renderer which in your case would be a JTree
Recommended reading http://docs.oracle.com/javase/tutorial/uiswing/components/list.html#renderer
 
Darryl Burke
Bartender
Posts: 5125
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just remember that items in a JList can't be edited, so you won't be able to interact with JTrees displayed in a JList.

I would use a vertical Box or a JPanel with a GridLayout(0, 1) in a JScrollPane. Either approach might require some fancy handling when tree nodes are expanded or collapsed.
 
Volodymyr Lysenko
Ranch Hand
Posts: 511
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Darryl Burke you are right.

I created simple Renderer as suggested in tutorial and tree does not expand its nodes.

I will try at first vertical Box
 
Volodymyr Lysenko
Ranch Hand
Posts: 511
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unfortunately the tree's leaves are not displayed but hidden by background color of whole Box.
I provided image of this.
The root of tree is displayed on white background but text of leaves is hidden by gray background of Box.

How to tell JTree to display itself including text of leaves?
How to add plus icon to collapsed nodes?
Filename: JTree.bmp
Description:
File size: 307 Kbytes
[Download JTree.bmp] Download Attachment
 
m Korbel
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
crossposted on SO

edit

- read Oracle tutorial how to use Tree, for working examples

- for better help sooner post an SSCCE, short, runnable, compilable, generated a.m. issue
 
Volodymyr Lysenko
Ranch Hand
Posts: 511
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
UNFORTUNATELY coderanch does not allow to post image into post
but forces somebody to download it.
This is nearly impossible to believe that somebody will ever download my image.
Without image nothing is clear about problem.
 
m Korbel
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
- notice not attack, nor intention

- not has nothing to do, something with CodeRanch or SO,

- has

1. answers are everywhere based on question

2. empty question equals wider answers, sugestions, guessing

3. question based on image, without an SSCCE is by default hardly answered, event suggestions, guessing on both forums are quite correct, good and clear described

4. goog answers are based on your effort (description, SSCCE, screenshort, asking for potential solutions, common workaround, e.i. bugs)

5. nobody nowhere will be bothering with to simulating of your issue, based only on image



 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic