Java核心技术II-第6章-可编辑的树-源代码

Java核心技术II-第6章-可编辑的树-源代码,在博文的最后有运行效果图。

 

package javaee.vii.ch06;

import java.awt.EventQueue;
import javax.swing.JFrame;

/**
 * This program demonstrates the editing.
 * @author lcwell
 */
public class TreeEditTest {
    
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                JFrame frame = new TreeEditFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setVisible(true);
            }
        });
    }
}

 

package javaee.vii.ch06;

import java.awt.BorderLayout;
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.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;

/**
 * A frame with a tree and buttons to edit the tree.
 * @author lcwell
 */


public class TreeEditFrame extends JFrame {

    public TreeEditFrame() {
        setTitle("TreeEditTest");
        setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

        // construct tree
        TreeNode root = makeSampleTree();
        model = new DefaultTreeModel(root);
        tree = new JTree(model);
        tree.setEditable(true);

        // add scroll pane with tree
        JScrollPane scrollPane = new JScrollPane(tree);
        add(scrollPane, BorderLayout.CENTER);

        makeButtons();
    }

    public TreeNode makeSampleTree() {
        DefaultMutableTreeNode root = new DefaultMutableTreeNode("世界");
        DefaultMutableTreeNode country = new DefaultMutableTreeNode("中国");
        root.add(country);
        DefaultMutableTreeNode state = new DefaultMutableTreeNode("广东省");
        country.add(state);
        state.add(new DefaultMutableTreeNode("珠海市"));
        state.add(new DefaultMutableTreeNode("广州市"));
        state = new DefaultMutableTreeNode("湖南省");
        country.add(state);
        state.add(new DefaultMutableTreeNode("益阳市"));
        country = new DefaultMutableTreeNode("美国");
        root.add(country);
        state = new DefaultMutableTreeNode("加利福尼亚州");
        country.add(state);
        state.add(new DefaultMutableTreeNode("纽约"));
        return root;
    }

    /**
     * Makes the buttons to add a sibling, add a child, and delete a node.
     */
    public void makeButtons() {
        JPanel panel = new JPanel();
        JButton addSiblingButton = new JButton("Add Sibling");
        addSiblingButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent event) {
                DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
                if (selectedNode == null) {
                    return;
                }

                DefaultMutableTreeNode parent = (DefaultMutableTreeNode) selectedNode.getParent();
                if (parent == null) {
                    return;
                }

                DefaultMutableTreeNode newNode = new DefaultMutableTreeNode("New");
                int selectIndex = parent.getIndex(selectedNode);
                model.insertNodeInto(newNode, parent, selectIndex + 1);

                // now display new node
                TreeNode[] nodes = model.getPathToRoot(newNode);
                TreePath path = new TreePath(nodes);
                tree.scrollPathToVisible(path);
            }
        });
        panel.add(addSiblingButton);

        JButton addChildButton = new JButton("Add Child");
        addChildButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent event) {
                DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
                if (selectedNode == null) {
                    return;
                }

                DefaultMutableTreeNode newNode = new DefaultMutableTreeNode("New");
                model.insertNodeInto(newNode, selectedNode, selectedNode.getChildCount());

                // now display new node
                TreeNode[] nodes = model.getPathToRoot(newNode);
                TreePath path = new TreePath(nodes);
                tree.scrollPathToVisible(path);
            }
        });
        panel.add(addChildButton);

        JButton deleteButton = new JButton("Delete");
        deleteButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent event) {
                DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
                if (selectedNode != null && selectedNode.getParent() != null) {
                    model.removeNodeFromParent(selectedNode);
                }
            }
        });
        panel.add(deleteButton);
        add(panel, BorderLayout.SOUTH);
    }

    private DefaultTreeModel model;
    private JTree tree;
    private static final int DEFAULT_WIDTH = 400;
    private static final int DEFAULT_HEIGHT = 200;
}

 


 Java核心技术II-06 一棵可编辑的树

你可能感兴趣的:(java)