Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Turning JTree into a layout manager, resizing nodes  RSS feed

 
Szab� �rp�d
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi.

I'd like to turn a JTree into a kind of layout manager by inserting JPanels as nodes.

These JPanels have their own layout managers of course, which tells what components have to resize themselves according to the JPanels' width.

This is not enough however, because in a JTree all the nodes take only their default spaces, so if I have a wider JPanel, then the less wider will not follow it in width.

I'd like those JPanels to take horizontally all the space provided by JTree: effectively all the JPanel nodes which are on the same level of hierarchy should have the same width, and this width would be the the width of the widest JPanel).

The problem in fact is independent of Panels (which I know can be achieved by e.g. a customized DefaultTreeCellRenderer), so I'd like to achieve to have those JLabel nodes to resize (themselves? or via a customized DefaultTreeCellRenderer? or ???), and I've no idea, how.

Oh, and the main point: if I resize the container panel in which the JTree (the JScrollPane) resides, the JTree and the contained nodes should be resized accordingly...

Hope this makes some sense, any clue is appreciated, thank you.

Arpad
[ June 20, 2007: Message edited by: Szab� �rp�d Zolt�n ]
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Szab� �rp�d
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
WOW! That's what I call a "clue"! Thank you.

I noticed however, that JPanels don't fit vertically if e.g. I add two additional JLabels to it like this:



Maybe the JPanels don't give their preferredSize correctly? Or it is simply not taken into account somehow in NodeDimensionsHandler where I see the resizing logic.

In the meantime, another idea came up: using a tree-table combo for a task like this. I guess the first level of tree could be a title, and the panels could be inserted into the second level, as a table cell. As far as I can see from this SUN treeTable tutorial , the table takes all the space, just like I imagined. What do you think?

Arpad
[ June 21, 2007: Message edited by: Szab� �rp�d Zolt�n ]
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JPanels don't fit vertically
I was focused on the width matching the parent container width and never thought about expanding the rows to fill the vertical space.
Maybe the JPanels don't give their preferredSize correctly? Or it is simply not taken into account somehow in NodeDimensionsHandler where I see the resizing logic
Number 2: I used JTree.setRowHeight instead of changes to the plaf code.
using a tree-table
Sounds like a good idea.
 
Szab� �rp�d
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All right, I could comment setRowHeigh out, and set it to the appropriate value. Rendered JPanel is resized correctly, just like I wanted, but I couldn't manage the editor to be resized. I tried to change the lines for editor as you did for the renderer:


No success.

I went on and looked into the possibilities of adding custom components to a treetable. It seemed too tough of a task for me. And in the meantime, I've found out, that the same treetable (described in a rather old article I mentioned before) exists as org.jdesktop.swingx.JXTreeTable, and this is a more sophisticated version and continually developed. It seems to me that adding a custom cellrenderer and celleditor is still hard, or at least requires some higher experience level.

Nota Bene: I've found a similar thing (a JTable (maybe a JTable on a JPanel??) as a node of a tree-table) in JIDE (can be watched in action in JIDE's demo at JIDE Grids-> HierarchicalTable (Nested Table)). This JIDE component is not open sourced. At least it became clear that it is not impossible!

And the bottom line: while tasting org.jdesktop.swingx.JXTreeTable and watching org.jdesktop demos, I've found org.jdesktop.swingx.JXTaskPane and JXTaskPaneContainer which gives the exact behavior and more, for what I had started to look into JTree in the beginning.

Maybe there would be people here who could tell me all this, but I didn't ask for this in fact, and I don't mind anyway, since I learned a lot about JTree and JTable. Thanks for the response, and thanks for javaranch.com, keep up the good work.

�rp�d

[ June 29, 2007: Message edited by: Szab� �rp�d Zolt�n ]
[ June 29, 2007: Message edited by: Szab� �rp�d Zolt�n ]
 
Tim Dudgeon
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just came across this example and found it really useful.
I find one limitation though. If the tree is currently being editing when it is re-sized setting the TreeUI again as a result of the componentResized() event causes loss of editing and loss of focus within the TreeCellEditor.

Is is possible to get the same effect without having to set the UI again?

Tim
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!