FileDocCategorySizeDatePackage
TreeEvents.javaAPI DocExample2863Thu Oct 24 20:14:26 BST 2002None

TreeEvents

public class TreeEvents extends JFrame implements TreeSelectionListener

Fields Summary
JButton
addB
JButton
deleteB
JTree
tree
DefaultMutableTreeNode
leadSelection
Constructors Summary
public TreeEvents()

    super("Tree Event Demo");
    setSize(300,200);
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    EEL eel = EEL.getInstance();
    eel.addGui();

    tree = new JTree();
    tree.setExpandsSelectedPaths(true);
    tree.setEditable(true);
    getContentPane().add(new JScrollPane(tree), BorderLayout.CENTER);
    tree.addTreeSelectionListener(eel);
    tree.addTreeSelectionListener(this);
    tree.addTreeExpansionListener(eel);
    tree.addTreeWillExpandListener(eel);
    tree.addPropertyChangeListener(eel);
    tree.getModel().addTreeModelListener(eel);

    addB = new JButton("Add a node");
    deleteB = new JButton("Delete a node");

    JPanel buttonP = new JPanel();
    buttonP.add(addB);
    buttonP.add(deleteB);
    getContentPane().add(buttonP, BorderLayout.SOUTH);

    addB.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
          String nodeName = JOptionPane.showInputDialog("New node name:");
          if (leadSelection != null) {
            leadSelection.add(new DefaultMutableTreeNode(nodeName));
            ((DefaultTreeModel)tree.getModel()).reload(leadSelection);
          }
          else {
            JOptionPane.showMessageDialog(TreeEvents.this, "No Parent...");
          }
        }
      });

    deleteB.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
          if (leadSelection != null) {
            DefaultMutableTreeNode parent = 
              (DefaultMutableTreeNode) leadSelection.getParent();
            if (parent == null) {
              JOptionPane.showMessageDialog(TreeEvents.this, 
                                            "Can't delete root");
            }
            else {
              parent.remove(leadSelection);
              leadSelection = null;
              ((DefaultTreeModel)tree.getModel()).reload(parent);
            }
          }
          else {
            JOptionPane.showMessageDialog(TreeEvents.this, "No Selection...");
          }
        }
      });
    eel.showDialog();
  
Methods Summary
public static voidmain(java.lang.String[] args)

    TreeEvents te = new TreeEvents();
    te.setVisible(true);
  
public voidvalueChanged(javax.swing.event.TreeSelectionEvent e)

    TreePath leadPath = e.getNewLeadSelectionPath();
    if (leadPath != null) {
      leadSelection = (DefaultMutableTreeNode)leadPath.getLastPathComponent();
    }