【Java-GUI】10 Swing05 JTree

简单的Tree结构:

package cn.dzz;

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;

public class JTree {
    JFrame jFrame = new JFrame("简单树状");
    
    public static void main(String[] args) {
        new JTree().init();
    }

    private void init() {
        // 创建节点对象
        DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode("根节点");

        DefaultMutableTreeNode firstLevelNode1 = new DefaultMutableTreeNode("一级节点1");
        DefaultMutableTreeNode firstLevelNode2 = new DefaultMutableTreeNode("一级节点2");
        DefaultMutableTreeNode firstLevelNode3 = new DefaultMutableTreeNode("一级节点3");

        DefaultMutableTreeNode secondLevelNode1 = new DefaultMutableTreeNode("二级节点1");
        DefaultMutableTreeNode secondLevelNode2 = new DefaultMutableTreeNode("二级节点2");
        DefaultMutableTreeNode secondLevelNode3 = new DefaultMutableTreeNode("二级节点3");
        DefaultMutableTreeNode secondLevelNode4 = new DefaultMutableTreeNode("二级节点4");
        DefaultMutableTreeNode secondLevelNode5 = new DefaultMutableTreeNode("二级节点5");
        DefaultMutableTreeNode secondLevelNode6 = new DefaultMutableTreeNode("二级节点6");
        DefaultMutableTreeNode secondLevelNode7 = new DefaultMutableTreeNode("二级节点7");
        DefaultMutableTreeNode secondLevelNode8 = new DefaultMutableTreeNode("二级节点8");
        DefaultMutableTreeNode secondLevelNode9 = new DefaultMutableTreeNode("二级节点9");
        // 按自定义的关系进行组装
        rootNode.add(firstLevelNode1);
        rootNode.add(firstLevelNode2);
        rootNode.add(firstLevelNode3);

        firstLevelNode1.add(secondLevelNode1);
        firstLevelNode1.add(secondLevelNode2);
        firstLevelNode1.add(secondLevelNode3);

        firstLevelNode2.add(secondLevelNode4);
        firstLevelNode2.add(secondLevelNode5);
        firstLevelNode2.add(secondLevelNode6);

        firstLevelNode3.add(secondLevelNode7);
        firstLevelNode3.add(secondLevelNode8);
        firstLevelNode3.add(secondLevelNode9);

        // 创建JTree对象进行装填
        javax.swing.JTree jTree = new javax.swing.JTree(rootNode);
        jFrame.add(jTree);

        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 点击关闭按钮结束程序
        jFrame.pack(); // 窗口自适应
        jFrame.setVisible(true);
    }

}

程序效果:

 

设置样式:

// 设置外观样式:
jTree.putClientProperty("JTree.lineStyle", "None");

设置移除节点关系线

设置一级节点水平分割线:

 // 设置外观样式:
jTree.putClientProperty("JTree.lineStyle", "Horizontal");

 

对节点的操作控制:

package cn.dzz;

import javax.swing.*;
import javax.swing.tree.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class JTree {
    JFrame jFrame = new JFrame("简单树状");
    // 创建节点对象
    DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode("根节点");

    DefaultMutableTreeNode firstLevelNode1 = new DefaultMutableTreeNode("一级节点1");
    DefaultMutableTreeNode firstLevelNode2 = new DefaultMutableTreeNode("一级节点2");
    DefaultMutableTreeNode firstLevelNode3 = new DefaultMutableTreeNode("一级节点3");

    DefaultMutableTreeNode secondLevelNode1 = new DefaultMutableTreeNode("二级节点1");
    DefaultMutableTreeNode secondLevelNode2 = new DefaultMutableTreeNode("二级节点2");
    DefaultMutableTreeNode secondLevelNode3 = new DefaultMutableTreeNode("二级节点3");
    DefaultMutableTreeNode secondLevelNode4 = new DefaultMutableTreeNode("二级节点4");
    DefaultMutableTreeNode secondLevelNode5 = new DefaultMutableTreeNode("二级节点5");
    DefaultMutableTreeNode secondLevelNode6 = new DefaultMutableTreeNode("二级节点6");
    DefaultMutableTreeNode secondLevelNode7 = new DefaultMutableTreeNode("二级节点7");
    DefaultMutableTreeNode secondLevelNode8 = new DefaultMutableTreeNode("二级节点8");
    DefaultMutableTreeNode secondLevelNode9 = new DefaultMutableTreeNode("二级节点9");
    // 操作按钮
    JButton btnForAddBrotherNode = new JButton("添加兄弟节点");
    JButton btnForAddSubNode = new JButton("添加子节点");
    JButton btnForDeleteThisNode = new JButton("删除此节点");
    JButton btnForChangeThisNode = new JButton("更改此节点");

    public static void main(String[] args) {
        new JTree().init();
    }

    private void init() {

        // 按自定义的关系进行组装
        rootNode.add(firstLevelNode1);
        rootNode.add(firstLevelNode2);
        rootNode.add(firstLevelNode3);

        firstLevelNode1.add(secondLevelNode1);
        firstLevelNode1.add(secondLevelNode2);
        firstLevelNode1.add(secondLevelNode3);

        firstLevelNode2.add(secondLevelNode4);
        firstLevelNode2.add(secondLevelNode5);
        firstLevelNode2.add(secondLevelNode6);

        firstLevelNode3.add(secondLevelNode7);
        firstLevelNode3.add(secondLevelNode8);
        firstLevelNode3.add(secondLevelNode9);

        // 创建JTree对象进行装填
        javax.swing.JTree jTree = new javax.swing.JTree(rootNode);

        // 设置节点树可以被操作
        jTree.setEditable(true);
        // 获取节点模型对象
        DefaultTreeModel model = (DefaultTreeModel)jTree.getModel();
        // 强制转换成实现类对象
        DefaultTreeModel defaultTreeModel = (DefaultTreeModel)model;

        // 添加操作
        btnForAddBrotherNode.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 获取当前选中的节点

                // 通过获取到的当前节点获取该节点的父节点
                Object lastSelectedPathComponent = jTree.getLastSelectedPathComponent();
                DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode)lastSelectedPathComponent;
                if (selectedNode == null) {
                    return;
                }
                // 创建一个新节点
                TreeNode parent = selectedNode.getParent();
                DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode)parent;
                if (parentNode == null) {
                    return;
                }
                // 把这个新节点交给父节点进行添加
                DefaultMutableTreeNode newNode = new DefaultMutableTreeNode("新节点");
                int selectedIndex = parentNode.getIndex(selectedNode);
                model.insertNodeInto(newNode, parentNode, selectedIndex);

                // 在节点树中显示这个节点
                TreeNode[] pathToRoot = model.getPathToRoot(newNode);
                TreePath treePath = new TreePath(pathToRoot);
                jTree.scrollPathToVisible(treePath);

                // 重新绘制Tree节点
                jTree.updateUI();
            }
        });

        btnForAddSubNode.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) jTree.getLastSelectedPathComponent();
                DefaultMutableTreeNode newNode = new DefaultMutableTreeNode("新增的子节点");

                selectedNode.add(newNode);

                TreeNode[] pathToRoot = model.getPathToRoot(newNode);
                TreePath treePath = new TreePath(pathToRoot);
                jTree.scrollPathToVisible(treePath);

                jTree.updateUI();
            }
        });

        btnForDeleteThisNode.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) jTree.getLastSelectedPathComponent();
                if (selectedNode != null && selectedNode.getParent() != null) {
                    model.removeNodeFromParent(selectedNode);
                }
            }
        });

        btnForChangeThisNode.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 获取当前选中的结点路径
                TreePath selectionPath = jTree.getSelectionPath();
                // 如果路径非空,则设置该路径的最后一个节点可以编辑
                if (selectionPath != null) {
                    jTree.startEditingAtPath(selectionPath);
                }
            }
        });

        jFrame.add(new JScrollPane(jTree));
        JPanel jPanel = new JPanel();
        jPanel.add(btnForAddBrotherNode);
        jPanel.add(btnForAddSubNode);
        jPanel.add(btnForChangeThisNode);
        jPanel.add(btnForDeleteThisNode);
        jFrame.add(jPanel, BorderLayout.SOUTH);
        // jFrame.add(jTree);

        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 点击关闭按钮结束程序
        jFrame.pack(); // 窗口自适应
        jFrame.setVisible(true);
    }

}

程序效果:

设置节点树的选择模式,单选,多选等等,然后监听选中的节点设置的值:

package cn.dzz;

import javax.swing.*;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class JTree {
    JFrame jFrame = new JFrame("简单树状");
    // 创建节点对象
    DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode("根节点");

    DefaultMutableTreeNode firstLevelNode1 = new DefaultMutableTreeNode("一级节点1");
    DefaultMutableTreeNode firstLevelNode2 = new DefaultMutableTreeNode("一级节点2");
    DefaultMutableTreeNode firstLevelNode3 = new DefaultMutableTreeNode("一级节点3");

    DefaultMutableTreeNode secondLevelNode1 = new DefaultMutableTreeNode("二级节点1");
    DefaultMutableTreeNode secondLevelNode2 = new DefaultMutableTreeNode("二级节点2");
    DefaultMutableTreeNode secondLevelNode3 = new DefaultMutableTreeNode("二级节点3");
    DefaultMutableTreeNode secondLevelNode4 = new DefaultMutableTreeNode("二级节点4");
    DefaultMutableTreeNode secondLevelNode5 = new DefaultMutableTreeNode("二级节点5");
    DefaultMutableTreeNode secondLevelNode6 = new DefaultMutableTreeNode("二级节点6");
    DefaultMutableTreeNode secondLevelNode7 = new DefaultMutableTreeNode("二级节点7");
    DefaultMutableTreeNode secondLevelNode8 = new DefaultMutableTreeNode("二级节点8");
    DefaultMutableTreeNode secondLevelNode9 = new DefaultMutableTreeNode("二级节点9");
    // 操作按钮
    JButton btnForAddBrotherNode = new JButton("添加兄弟节点");
    JButton btnForAddSubNode = new JButton("添加子节点");
    JButton btnForDeleteThisNode = new JButton("删除此节点");
    JButton btnForChangeThisNode = new JButton("更改此节点");

    // 文本框
    JTextArea jTextArea = new JTextArea(5, 20);

    public static void main(String[] args) {
        new JTree().init();
    }

    private void init() {

        // 按自定义的关系进行组装
        rootNode.add(firstLevelNode1);
        rootNode.add(firstLevelNode2);
        rootNode.add(firstLevelNode3);

        firstLevelNode1.add(secondLevelNode1);
        firstLevelNode1.add(secondLevelNode2);
        firstLevelNode1.add(secondLevelNode3);

        firstLevelNode2.add(secondLevelNode4);
        firstLevelNode2.add(secondLevelNode5);
        firstLevelNode2.add(secondLevelNode6);

        firstLevelNode3.add(secondLevelNode7);
        firstLevelNode3.add(secondLevelNode8);
        firstLevelNode3.add(secondLevelNode9);

        // 创建JTree对象进行装填
        javax.swing.JTree jTree = new javax.swing.JTree(rootNode);

        // 设置节点树可以被操作
        jTree.setEditable(true);
        // 获取节点模型对象
        DefaultTreeModel model = (DefaultTreeModel)jTree.getModel();
        // 强制转换成实现类对象
        DefaultTreeModel defaultTreeModel = (DefaultTreeModel)model;

        // 添加操作
        btnForAddBrotherNode.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 获取当前选中的节点

                // 通过获取到的当前节点获取该节点的父节点
                Object lastSelectedPathComponent = jTree.getLastSelectedPathComponent();
                DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode)lastSelectedPathComponent;
                if (selectedNode == null) {
                    return;
                }
                // 创建一个新节点
                TreeNode parent = selectedNode.getParent();
                DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode)parent;
                if (parentNode == null) {
                    return;
                }
                // 把这个新节点交给父节点进行添加
                DefaultMutableTreeNode newNode = new DefaultMutableTreeNode("新节点");
                int selectedIndex = parentNode.getIndex(selectedNode);
                model.insertNodeInto(newNode, parentNode, selectedIndex);

                // 在节点树中显示这个节点
                TreeNode[] pathToRoot = model.getPathToRoot(newNode);
                TreePath treePath = new TreePath(pathToRoot);
                jTree.scrollPathToVisible(treePath);

                // 重新绘制Tree节点
                jTree.updateUI();
            }
        });

        btnForAddSubNode.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) jTree.getLastSelectedPathComponent();
                DefaultMutableTreeNode newNode = new DefaultMutableTreeNode("新增的子节点");

                selectedNode.add(newNode);

                TreeNode[] pathToRoot = model.getPathToRoot(newNode);
                TreePath treePath = new TreePath(pathToRoot);
                jTree.scrollPathToVisible(treePath);

                jTree.updateUI();
            }
        });

        btnForDeleteThisNode.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) jTree.getLastSelectedPathComponent();
                if (selectedNode != null && selectedNode.getParent() != null) {
                    model.removeNodeFromParent(selectedNode);
                }
            }
        });

        btnForChangeThisNode.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 获取当前选中的结点路径
                TreePath selectionPath = jTree.getSelectionPath();
                // 如果路径非空,则设置该路径的最后一个节点可以编辑
                if (selectionPath != null) {
                    jTree.startEditingAtPath(selectionPath);
                }
            }
        });

        // 设置节点树的选择模式
        TreeSelectionModel selectionModel = jTree.getSelectionModel();
        selectionModel.setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);

        // 设置监听的选择器
        jTree.addTreeSelectionListener(new TreeSelectionListener() {
            @Override
            public void valueChanged(TreeSelectionEvent e) {
                TreePath newLeadSelectionPath = e.getNewLeadSelectionPath();
                jTextArea.append(newLeadSelectionPath.toString() + "\n");
            }
        });


        Box horizontalBox = Box.createHorizontalBox();
        horizontalBox.add(new JScrollPane(jTree));
        horizontalBox.add(new JScrollPane(jTextArea));
        jFrame.add(horizontalBox);

        JPanel jPanel = new JPanel();
        jPanel.add(btnForAddBrotherNode);
        jPanel.add(btnForAddSubNode);
        jPanel.add(btnForChangeThisNode);
        jPanel.add(btnForDeleteThisNode);
        jFrame.add(jPanel, BorderLayout.SOUTH);
        // jFrame.add(jTree);

        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 点击关闭按钮结束程序
        jFrame.pack(); // 窗口自适应
        jFrame.setVisible(true);
    }

}

程序效果:

 

posted @ 2021-03-10 18:34  emdzz  阅读(222)  评论(0编辑  收藏  举报