aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Widgets hierarchy Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Widgets hierarchy" Watch "Widgets hierarchy" New topic

Widgets hierarchy

Joaquin Morcate

Joined: Nov 17, 2005
Posts: 3

I'm building a JTree that uses a custom renderer. This renderer creates a number of other widgets to display a node: a JPanel, a button, a JTable, ... The tree is displayed as usual in a JScrollPane contained in a JFrame. Everything is displayed as spected.

But if I try to go down the wiget hierarchy from the JFrame using the getComponents method from Container I cannot find the components created by the renderer. The only child of the JTree happens to be just one CellRenderPanel that has no components itself.

The question is where are those components and how can I access them???

Thanks a lot for your help.

(Actually I'm trying to build a JTree which nodes can be expanded. So each node includes a button to hide/show details. I want to selected the display mode without entering in edition mode by clicking on the button. Edition mode is activated by double click. )
Nathan Pruett

Joined: Oct 18, 2000
Posts: 4121

Renderers in Swing don't follow the regular component hierarchy - for example, if you have a JLabel and a JButton in your tree cell renderer, there's no way through the component hierarchy to drill down from the frame->content pane->tree->label and just change the icon on the label in the 3rd child of the root node. Renderers basically are just "image snapshots" of the renderer components with the specific data needed for that tree/table/list item. You can have renderers change in response to events - but you have to change data in the model, and have the renderer "render" differently because of the changed data in the model.

I don't follow the "display mode/edition mode" description of what you want to happen, but it sounds like whatever model is behind your tree nodes needs to either have an "expanded" property to tell the renderer to render differently, or you need to have 2 different classes in your model that different renderers are bound to, and swap the model objects out in your tree nodes when you want to change how they look.

Write once, run anywhere, because there's nowhere to hide! - /. A.C.
I agree. Here's the link:
subject: Widgets hierarchy