Java Tips Weblog

  • Blog Stats

    • 2,065,823 hits
  • Categories

  • Archives

  • Advertisements

Component Tree Model

Posted by Darryl Burke on November 15, 2008

The code for a complex GUI can run into a few hundred lines. When maintaining someone else’s GUI code, or even your own code that you haven’t visited for some time, the ability to quickly identify the nesting of the various components can save much code-searching.

ComponentTreeModel attempts to address this need by reproducing the hierarchy of a GUI as a TreeModel.

There are two distinct ways to use ComponentTreeModel

  • To construct a new ComponentTreeModel with a JComponent, typically the JRootPane of a JFrame or JDialog, as its root
  • To add the GUI hierarchy subordinate to a specified node of an existing DefaultTreeModel, by invoking the static method getComponentTreeModel.

The supplied example showcases the usage of ComponentTreeModel. Run the code and click the button or select the first menu item.



Notice that the internals of the JComboBox and JScrollPane can be examined, and that JMenuItems aren’t a part of a frame’s hierarchy but are separately shown in a JPopupMenu.


In a future posting, ComponentTreeCellRenderer will provide a means to display images of the components in a tree that has a ComponentTreeModel.

Get The Code

See Also

Swing Utils
Component Tree Cell Renderer

Related Reading

Java API: javax.swing.tree.DefaultTreeModel
Java API: javax.swing.JTree


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: