FileDocCategorySizeDatePackage
JTreeDemo.javaAPI DocExample2164Sat Nov 25 12:55:14 GMT 2000None

JTreeDemo

public class JTreeDemo extends JFrame
Simple JFC JTree demo
version
$Id: JTreeDemo.java,v 1.3 2000/11/25 17:55:15 ian Exp $

Fields Summary
JButton
addButton
JButton
quitButton
JTree
myTree
DefaultMutableTreeNode
root
DefaultMutableTreeNode
child
Constructors Summary
public JTreeDemo()
Construct the object including its GUI

		super("JTreeDemo");
		Container cp = getContentPane();
		cp.setLayout(new BorderLayout());
			
		root = new DefaultMutableTreeNode("root");

		child = new DefaultMutableTreeNode("Colors"); 
		root.add(child);
		child.add(new DefaultMutableTreeNode("Cyan"));
		child.add(new DefaultMutableTreeNode("Magenta"));
		child.add(new DefaultMutableTreeNode("Yellow"));
		child.add(new DefaultMutableTreeNode("Black"));

		myTree = new JTree(root);

		cp.add(BorderLayout.CENTER, myTree);

		cp.add(BorderLayout.NORTH, addButton = new JButton("Add"));
		addButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

			// Insert more nodes into the tree
			child = new DefaultMutableTreeNode("Flavors");
			child.add(new DefaultMutableTreeNode("Java"));
			child.add(new DefaultMutableTreeNode("Espresso"));
			child.add(new DefaultMutableTreeNode("Hey Joe!"));
			child.add(new DefaultMutableTreeNode("Charcoal"));
			child.add(new DefaultMutableTreeNode("Paint Remover"));

			// Notify the model, which will add it and create an event, and
			// send it up the tree...

			((DefaultTreeModel)myTree.getModel()).insertNodeInto(child, root, 0);
			}
		});

		cp.add(BorderLayout.SOUTH, quitButton = new JButton("Exit"));
		quitButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				setVisible(false);
				dispose();
				System.exit(0);
			}
		});
		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				setVisible(false);
				dispose();
				System.exit(0);
			}
		});
		pack();
	
Methods Summary
public static voidmain(java.lang.String[] av)
"main program" method - construct and show

		// create a JTreeDemo object, tell it to show up
		new JTreeDemo().setVisible(true);