public void actionPerformed(ActionEvent e) { String command = e.getActionCommand(); if (ADD_COMMAND.equals(command)) { // Add button clicked treePanel.addObject("New Node " + newNodeSuffix++); } else if (REMOVE_COMMAND.equals(command)) { // Remove button clicked treePanel.removeCurrentNode(); } else if (CLEAR_COMMAND.equals(command)) { // Clear button clicked. treePanel.clear(); } }
public DynamicTreeDemo() { super(new BorderLayout()); // Create the components. treePanel = new DynamicTree(); populateTree(treePanel); JButton addButton = new JButton("Add"); addButton.setActionCommand(ADD_COMMAND); addButton.addActionListener(this); JButton removeButton = new JButton("Remove"); removeButton.setActionCommand(REMOVE_COMMAND); removeButton.addActionListener(this); JButton clearButton = new JButton("Clear"); clearButton.setActionCommand(CLEAR_COMMAND); clearButton.addActionListener(this); // Lay everything out. treePanel.setPreferredSize(new Dimension(300, 150)); add(treePanel, BorderLayout.CENTER); JPanel panel = new JPanel(new GridLayout(0, 3)); panel.add(addButton); panel.add(removeButton); panel.add(clearButton); add(panel, BorderLayout.SOUTH); }
public void populateTree(DynamicTree treePanel) { String p1Name = new String("Parent 1"); String p2Name = new String("Parent 2"); String c1Name = new String("Child 1"); String c2Name = new String("Child 2"); DefaultMutableTreeNode p1, p2; p1 = treePanel.addObject(null, p1Name); p2 = treePanel.addObject(null, p2Name); treePanel.addObject(p1, c1Name); treePanel.addObject(p1, c2Name); treePanel.addObject(p2, c1Name); treePanel.addObject(p2, c2Name); }