FileDocCategorySizeDatePackage
ComponentTree.javaAPI DocExample7299Sat Jan 24 10:44:34 GMT 2004je3.gui

ComponentTree

public class ComponentTree extends JTree
This class is a JTree subclass that displays the tree of AWT or Swing component that make up a GUI.

Fields Summary
Constructors Summary
public ComponentTree(Component c)
All this constructor method has to do is set the TreeModel and TreeCellRenderer objects for the tree. It is these classes (defined below) that do all the real work.

	super(new ComponentTreeModel(c));
	setCellRenderer(new ComponentCellRenderer(getCellRenderer()));
    
Methods Summary
public static voidmain(java.lang.String[] args)
This main() method demonstrates the use of the ComponentTree class: it puts a ComponentTree component in a Frame, and uses the ComponentTree to display its own GUI hierarchy. It also adds a TreeSelectionListener to display additional information about each component as it is selected

	// Create a frame for the demo, and handle window close requests
	JFrame frame = new JFrame("ComponentTree Demo");
	frame.addWindowListener(new WindowAdapter() {
		public void windowClosing(WindowEvent e) { System.exit(0); }
	    });

	// Create a scroll pane and a "message line" and add them to the
	// center and bottom of the frame.
	JScrollPane scrollpane = new JScrollPane();
	final JLabel msgline = new JLabel(" ");
	frame.getContentPane().add(scrollpane, BorderLayout.CENTER);
	frame.getContentPane().add(msgline, BorderLayout.SOUTH);

	// Now create the ComponentTree object, specifying the frame as the
	// component whose tree is to be displayed.  Also set the tree's font.
	JTree tree = new ComponentTree(frame);
	tree.setFont(new Font("SansSerif", Font.BOLD, 12));

	// Only allow a single item in the tree to be selected at once
	tree.getSelectionModel().setSelectionMode(
			    TreeSelectionModel.SINGLE_TREE_SELECTION);

	// Add an event listener for notifications when
	// the tree selection state changes. 
	tree.addTreeSelectionListener(new TreeSelectionListener() {
		public void valueChanged(TreeSelectionEvent e) {
		    // Tree selections are referred to by "path"
		    // We only care about the last node in the path
		    TreePath path = e.getPath();
		    Component c = (Component) path.getLastPathComponent();
		    // Now we know what component was selected, so
		    // display some information about it in the message line
		    if (c.isShowing()) {
			Point p = c.getLocationOnScreen();
			msgline.setText("x: " + p.x + "  y: " + p.y +
					"  width: " + c.getWidth() +
					"  height: " + c.getHeight());
		    }
		    else {
			msgline.setText("component is not showing");
		    }
		}
	    });

	// Now that we've set up the tree, add it to the scrollpane
	scrollpane.setViewportView(tree);

	// Finally, set the size of the main window, and pop it up.
	frame.setSize(600, 400);
	frame.setVisible(true);