怎样用程序来修改jTree中的某个节点的名字
答案:1 悬赏:80 手机版
解决时间 2021-04-02 11:20
- 提问者网友:练爱
- 2021-04-01 19:07
怎样用程序来修改jTree中的某个节点的名字
最佳答案
- 五星知识达人网友:西岸风
- 2021-04-01 20:32
估计这是你想要的结果
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.*;
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();
}
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();
}
});
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.*;
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();
}
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();
}
});
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯