怎样用程序来修改jTree中的某个节点的名字

 我来答
就烦条0o
2016-12-21 · 知道合伙人软件行家
就烦条0o
知道合伙人软件行家
采纳数:33315 获赞数:46492
从事多年系统运维,喜欢编写各种小程序和脚本。

向TA提问 私信TA
展开全部
估计这是你想要的结果
package yagumo.LayerOut.A;

import java.awt.*;
import javax.swing.*;
import java.awt.Rectangle;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeSelectionModel;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.tree.*;

/**
* <p>Title: 八云测试用例</p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2005</p>
*
* <p>Company: </p>
*
* @author yagumo
* @version 1.0
*/
public class ChangeNodeName extends JFrame {
public ChangeNodeName() {
try {
jbInit();
}
catch (Exception exception) {
exception.printStackTrace();
}
}

private void jbInit() throws Exception {
getContentPane().setLayout(null);
jTree1.setBounds(new Rectangle(54, 42, 289, 99));
jButton1.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
TreePath treePath = jTree1.getSelectionPath();
DefaultMutableTreeNode node = (DefaultMutableTreeNode) treePath.getLastPathComponent();
//改名
node.setUserObject("123");
DefaultTreeModel treeModel = (DefaultTreeModel) jTree1.getModel();
//刷新
treeModel.reload();
//重置选择支
jTree1.setSelectionPath(treePath);
}
});
this.getContentPane().add(jButton1);
this.getContentPane().add(jTree1);
jButton1.setBounds(new Rectangle(162, 178, 71, 25));
jButton1.setText("jButton1");
}

public static void main(String[] args) {
ChangeNodeName changenodename = new ChangeNodeName();
changenodename.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
changenodename.setSize(400, 300);
changenodename.show();
}
/**
* 生成JTreePath树型结构
* @return JTree
*/
private JTree getJTreePath() {
DefaultMutableTreeNode nodeTop = new DefaultMutableTreeNode("PeiGet");
DefaultMutableTreeNode nodeDownloading = new DefaultMutableTreeNode("正在下载");
nodeTop.add(nodeDownloading);

JTree jTree = new JTree(nodeTop);
jTree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
return jTree;
}

JTree jTree1 = getJTreePath();
JButton jButton1 = new JButton();
}

对我有用[0]
丢个板砖[0]
引用 |
举报 |

管理

关注

yagumo

yagumo

本版等级:

#6
得分:0

回复于:
2005-03-13 18:44:19

改进,刷新的时候直接jTree1.updateUI()就行了,都不用去还原先前的选择支的.
重来个按钮事件处理过程:
jButton1.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode) jTree1.getSelectionPath().getLastPathComponent();
//改名
node.setUserObject("123");
//刷新
jTree1.updateUI();
}
});
本回答被网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式