How can I change also the root handle pictures in a JTree component, possibly with little arrows instead of "handles"?

Jon Wingfield

The icons used to display the root handles on JTrees are specified in the UIDefaults Map for the installed UI LookAndFeel (LAF). These defaults can be changed from the default by using the swing UIManager. For a JTree with a UI installed which is a sub-class of javax.swing.plaf.basic.BasicTreeUI (a fairly safe assumption as all LAFs provided by SUN extend the BasicLookAndFeel) the keys in the UIDefaults Map "Tree.expandedIcon" and "Tree.collapsedIcon" have values which are the Icons used for display. Other UI "properties" stored in the LAF's UIDefaults can be found in the BasicLookAndFeel source.
The following code changes the icons for all subsequently instantiated trees:

Icon openIcon = new ImageIcon("images/openRoot.gif");
Icon closedIcon = new ImageIcon("images/openRoot.gif");
              new javax.swing.plaf.IconUIResource(closedIcon));
              new javax.swing.plaf.IconUIResource(openIcon));
To change the icons for one instance of a JTree then use the following code:
JTree tree = new JTree();
Icon openIcon = new ImageIcon("images/openRoot.gif");
Icon closedIcon = new ImageIcon("images/closedRoot.gif");
// assume UI is a sub-class of BasicTreeUI
To change the appearance of the Node and Leaf images is easier in that the JTree API caters for this (almost) directly. All that is needed is:
JTree tree = new JTree();
DefaultTreeCellRenderer tcr = (DefaultTreeCellRenderer)tree.getCellRenderer();
tcr.setOpenIcon(new ImageIcon("images/openNode.gif"));
tcr.setClosedIcon(new ImageIcon("images/closedNode.gif"));
  new ImageIcon("images/Leaf.gif"));
0 Comments  (click to add your comment)
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



About | Sitemap | Contact