Tree node editing with DefaultTreeModel : TreeModel « Swing « Java Tutorial

Home
Java Tutorial
1.Language
2.Data Type
3.Operators
4.Statement Control
5.Class Definition
6.Development
7.Reflection
8.Regular Expressions
9.Collections
10.Thread
11.File
12.Generics
13.I18N
14.Swing
15.Swing Event
16.2D Graphics
17.SWT
18.SWT 2D Graphics
19.Network
20.Database
21.Hibernate
22.JPA
23.JSP
24.JSTL
25.Servlet
26.Web Services SOA
27.EJB3
28.Spring
29.PDF
30.Email
31.J2ME
32.J2EE Application
33.XML
34.Design Pattern
35.Log
36.Security
37.Apache Common
38.Ant
39.JUnit
Java Tutorial » Swing » TreeModel 




import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.MutableTreeNode;
import javax.swing.tree.TreePath;

public class MainClass {
  public static void main(String[] args) {
    MutableTreeNode root = new DefaultMutableTreeNode("A");
    MutableTreeNode beams = new DefaultMutableTreeNode("B");
    MutableTreeNode gears = new DefaultMutableTreeNode("C");
    root.insert(beams, 0);
    root.insert(gears, 1);
    beams.insert(new DefaultMutableTreeNode("4 ")0);
    beams.insert(new DefaultMutableTreeNode("6 ")1);
    beams.insert(new DefaultMutableTreeNode("8 ")2);
    beams.insert(new DefaultMutableTreeNode("12 ")3);
    gears.insert(new DefaultMutableTreeNode("8t")0);
    gears.insert(new DefaultMutableTreeNode("24t")1);
    gears.insert(new DefaultMutableTreeNode("40t")2);

    final DefaultTreeModel model = new DefaultTreeModel(root);
    final JTree tree = new JTree(model);

    final JTextField nameField = new JTextField("16t");
    final JButton button = new JButton("Add a part");
    button.setEnabled(false);
    button.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        TreePath tp = tree.getSelectionPath();
        MutableTreeNode insertNode = (MutableTreeNodetp.getLastPathComponent();
        int insertIndex = 0;
        if (insertNode.getParent() != null) {
          MutableTreeNode parent = (MutableTreeNodeinsertNode.getParent();
          insertIndex = parent.getIndex(insertNode1;
          insertNode = parent;
        }
        MutableTreeNode node = new DefaultMutableTreeNode(nameField.getText());
        model.insertNodeInto(node, insertNode, insertIndex);
      }
    });
    JPanel addPanel = new JPanel(new GridLayout(21));
    addPanel.add(nameField);
    addPanel.add(button);

    tree.addTreeSelectionListener(new TreeSelectionListener() {
      public void valueChanged(TreeSelectionEvent e) {
        TreePath tp = e.getNewLeadSelectionPath();
        button.setEnabled(tp != null);
      }
    });

    JFrame frame = new JFrame();

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(200200);
    frame.getContentPane().add(new JScrollPane(tree));
    frame.getContentPane().add(addPanel, BorderLayout.SOUTH);
    frame.setVisible(true);
  }
}














14.68.TreeModel
14.68.1.Tree node editing with DefaultTreeModel
14.68.2.implements TreeModelimplements TreeModel
14.68.3.Genealogy Tree
14.68.4.JTree.getModel().addTreeModelListener(new TreeModelListener())
14.68.5.Expand All for a tree path
14.68.6.Get tree path from TreeNode
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.