用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - java代码库

2017-12-26 作者: 柯侧耳倾听者举报

[java]代码库

import java.awt.BorderLayout;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.tree.*;
public class EditJTree
{
	JFrame jf;
	JTree tree;
	DefaultTreeModel model;
	DefaultMutableTreeNode root = new DefaultMutableTreeNode("中国");
	DefaultMutableTreeNode guangdong = new DefaultMutableTreeNode("广东");
	DefaultMutableTreeNode guangxi = new DefaultMutableTreeNode("广西");
	DefaultMutableTreeNode foshan = new DefaultMutableTreeNode("佛山");
	DefaultMutableTreeNode shantou = new DefaultMutableTreeNode("汕头");
	DefaultMutableTreeNode guilin = new DefaultMutableTreeNode("桂林");
	DefaultMutableTreeNode nanning = new DefaultMutableTreeNode("南宁");
	TreePath movePath;
	JButton addSiblingButton = new JButton("添加兄弟节点");
	JButton addChildButton = new JButton("添加子节点");
	JButton deleteButton = new JButton("删除节点");
	JButton editButton = new JButton("编辑当前节点");
	public void init()
	{
		guangdong.add(foshan);
		guangdong.add(shantou);
		guangxi.add(guilin);
		guangxi.add(nanning);
		root.add(guangdong);
		root.add(guangxi);
		jf = new JFrame("可编辑节点的树");
		tree = new JTree(root);
		model = (DefaultTreeModel)tree.getModel();
		tree.setEditable(true);
		MouseListener ml = new MouseAdapter()
		{
			public void mousePressed(MouseEvent e)
			{
				TreePath tp = tree.getPathForLocation(
					e.getX() , e.getY());
				if (tp != null)
				{
					movePath = tp;
				}
			}
			public void mouseReleased(MouseEvent e)
			{
				TreePath tp = tree.getPathForLocation(
					e.getX(), e.getY());
				if (tp != null && movePath != null)
				{
					if (movePath.isDescendant(tp) && movePath != tp)
					{
						JOptionPane.showMessageDialog(jf,
							"目标节点是被移动节点的子节点,无法移动!",
							"非法操作", JOptionPane.ERROR_MESSAGE );
						return;
					}
					else if (movePath != tp)
					{
						((DefaultMutableTreeNode)tp.getLastPathComponent())
							.add((DefaultMutableTreeNode)movePath
							.getLastPathComponent());
						movePath = null;
						tree.updateUI();
					}
				}
			}
		};
		tree.addMouseListener(ml);
		JPanel panel = new JPanel();
		addSiblingButton.addActionListener(event -> {
			DefaultMutableTreeNode selectedNode	= (DefaultMutableTreeNode)
				tree.getLastSelectedPathComponent();
			if (selectedNode == null) return;
			DefaultMutableTreeNode parent = (DefaultMutableTreeNode)
				selectedNode.getParent();
			if (parent == null) return;
			DefaultMutableTreeNode newNode = new
				DefaultMutableTreeNode("新节点");
			int selectedIndex = parent.getIndex(selectedNode);
			model.insertNodeInto(newNode, parent, selectedIndex + 1);
			TreeNode[] nodes = model.getPathToRoot(newNode);
			TreePath path = new TreePath(nodes);
			tree.scrollPathToVisible(path);
		});
		panel.add(addSiblingButton);
		addChildButton.addActionListener(event -> {
			DefaultMutableTreeNode selectedNode	= (DefaultMutableTreeNode)
				tree.getLastSelectedPathComponent();
			if (selectedNode == null) return;
			DefaultMutableTreeNode newNode = new
				DefaultMutableTreeNode("新节点");
			selectedNode.add(newNode);
			TreeNode[] nodes = model.getPathToRoot(newNode);
			TreePath path = new TreePath(nodes);
			tree.scrollPathToVisible(path);
			tree.updateUI();
		});
		panel.add(addChildButton);
		deleteButton.addActionListener(event ->	{
			DefaultMutableTreeNode selectedNode	= (DefaultMutableTreeNode)
				tree.getLastSelectedPathComponent();
			if (selectedNode != null && selectedNode.getParent() != null)
			{
				model.removeNodeFromParent(selectedNode);
			}
		});
		panel.add(deleteButton);
		editButton.addActionListener(event -> {
			TreePath selectedPath = tree.getSelectionPath();
			if (selectedPath != null)
			{
				tree.startEditingAtPath(selectedPath);
			}
		});
		panel.add(editButton);
		jf.add(new JScrollPane(tree));
		jf.add(panel , BorderLayout.SOUTH);
		jf.pack();
		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		jf.setVisible(true);
	}
	public static void main(String[] args)
	{
		new EditJTree().init();
	}
}


网友评论    (发表评论)

共1 条评论 1/1页

发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...