java 动态的给树添加新节点 望高手指点啊

最好能有具体的代码... 最好能有具体的代码 展开
 我来答
aaronswon
推荐于2017-12-16 · TA获得超过3481个赞
知道大有可为答主
回答量:1863
采纳率:0%
帮助的人:1616万
展开全部
//先选中节点才能增加节点
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.tree.*;

public class TreeTest implements ActionListener,TreeModelListener{
JLabel label=null;
JTree tree=null;
DefaultTreeModel treeModel=null;
String nodeName=null;//原有节点名称

public TreeTest(){
JFrame f=new JFrame("TreeTest");
Container contentPane=f.getContentPane();

DefaultMutableTreeNode root=new DefaultMutableTreeNode("资源管理器");

tree=new JTree(root);
tree.setEditable(true);
tree.addMouseListener(new MouseHandle());
treeModel=(DefaultTreeModel)tree.getModel();
treeModel.addTreeModelListener(this);

JScrollPane scrollPane=new JScrollPane();
scrollPane.setViewportView(tree);

JPanel panel=new JPanel();
JButton b=new JButton("新增节点");
b.addActionListener(this);
panel.add(b);
b=new JButton("删除节点");
b.addActionListener(this);
panel.add(b);
b=new JButton("清除所有节点");
b.addActionListener(this);
panel.add(b);

label=new JLabel("Action");
contentPane.add(panel,BorderLayout.NORTH);
contentPane.add(scrollPane,BorderLayout.CENTER);
contentPane.add(label,BorderLayout.SOUTH);
f.pack();
f.setVisible(true);
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
//本方法运行新增、删除、清除所有节点的程序代码.
public void actionPerformed(ActionEvent ae){
if (ae.getActionCommand().equals("新增节点")){
DefaultMutableTreeNode parentNode=null;
DefaultMutableTreeNode newNode=new DefaultMutableTreeNode("新节点");
newNode.setAllowsChildren(true);
TreePath parentPath=tree.getSelectionPath();

//取得新节点的父节点
parentNode=(DefaultMutableTreeNode)(parentPath.getLastPathComponent());

//由DefaultTreeModel的insertNodeInto()方法增加新节点
treeModel.insertNodeInto(newNode,parentNode,parentNode.getChildCount());

//tree的scrollPathToVisible()方法在使Tree会自动展开文件夹以便显示所加入的新节点。若没加这行则加入的新节点
//会被 包在文件夹中,你必须自行展开文件夹才看得到。
tree.scrollPathToVisible(new TreePath(newNode.getPath()));
label.setText("新增节点成功");
}
if (ae.getActionCommand().equals("删除节点")){
TreePath treepath=tree.getSelectionPath();
if (treepath!=null){
//下面两行取得选取节点的父节点.
DefaultMutableTreeNode selectionNode=(DefaultMutableTreeNode)treepath.getLastPathComponent();
TreeNode parent=(TreeNode)selectionNode.getParent();
if (parent!=null) {
//由DefaultTreeModel的removeNodeFromParent()方法删除节点,包含它的子节点。
treeModel.removeNodeFromParent(selectionNode);
label.setText("删除节点成功");
}
}
}
if (ae.getActionCommand().equals("清除所有节点")){

//下面一行,由DefaultTreeModel的getRoot()方法取得根节点.
DefaultMutableTreeNode rootNode=(DefaultMutableTreeNode)treeModel.getRoot();

//下面一行删除所有子节点.
rootNode.removeAllChildren();

//删除完后务必运行DefaultTreeModel的reload()操作,整个Tree的节点才会真正被删除.
treeModel.reload();
label.setText("清除所有节点成功");
}
}
public void treeNodesChanged(TreeModelEvent e){
TreePath treePath=e.getTreePath();
DefaultMutableTreeNode node=(DefaultMutableTreeNode)treePath.getLastPathComponent();
try{
int[] index=e.getChildIndices();
node=(DefaultMutableTreeNode)node.getChildAt(index[0]);
}catch(NullPointerException exc){}
label.setText(nodeName+"更改数据为:"+(String)node.getUserObject());
}
public void treeNodesInserted(TreeModelEvent e){
System.out.println("new node insered");
}
public void treeNodesRemoved(TreeModelEvent e){
System.out.println("node deleted");
}
public void treeStructureChanged(TreeModelEvent e){
System.out.println("Structrue changed");
}
public static void main(String[] args){
new TreeTest();
}

class MouseHandle extends MouseAdapter{
public void mousePressed(MouseEvent e){
try{
JTree tree=(JTree)e.getSource();
int rowLocation=tree.getRowForLocation(e.getX(),e.getY());
TreePath treepath=tree.getPathForRow(rowLocation);
TreeNode treenode=(TreeNode)treepath.getLastPathComponent();
nodeName=treenode.toString();
}catch(NullPointerException ne){}
}
}
}
zhangxu1216
2009-03-24 · 超过41用户采纳过TA的回答
知道小有建树答主
回答量:173
采纳率:0%
帮助的人:82.9万
展开全部
这个我已经调试好了,你运行看看

import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeSelectionModel;

public class Tree extends JFrame{
private int i=0;

public Tree() {

DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
DefaultMutableTreeNode child = new DefaultMutableTreeNode("Child");
DefaultMutableTreeNode select = new DefaultMutableTreeNode("select");

DefaultMutableTreeNode child1 = new DefaultMutableTreeNode("" + i);

root.add(child);
root.add(select);
child.add(child1);

JTree tree = new JTree(root);
tree.getSelectionModel().setSelectionMode(
TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);

// 每个节点的行高
tree.setRowHeight(20);
tree.addTreeSelectionListener(new TreeSelectionListener() {
public void valueChanged(TreeSelectionEvent e) {
// 内隐类不能直接引用外部类tree,1.外部变量可申明为final 2.新建外部类的对象
JTree tree = (JTree) e.getSource();
DefaultMutableTreeNode selectNode = (DefaultMutableTreeNode) tree
.getLastSelectedPathComponent();
i++;
selectNode.add(new DefaultMutableTreeNode("" + i));
// Object s = tree.getLastSelectedPathComponent();//
// getSelectedValue();
// JOptionPane.showMessageDialog(null, s + " is selected",
// "Message !", JOptionPane.YES_OPTION);
}
});

tree.setPreferredSize(new Dimension(100, 300));
// tree.setEnabled(true);
JScrollPane scrollPane = new JScrollPane(tree);
// scrollPane.setSize(100,350);

this.getContentPane().add(scrollPane);
}
public static void main(String[] args) {
Tree tree=new Tree();
tree.getContentPane().add(new JLabel("当你点击叶节点的时候,树会自动扩展!"),BorderLayout.NORTH);
tree.setSize(400, 300);
tree.setVisible(true);

}

}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
雪箂
2009-03-24 · TA获得超过502个赞
知道小有建树答主
回答量:471
采纳率:57%
帮助的人:84万
展开全部
你首先要说明白是javascript还是java swt/rcp还是什么其他的地方用.
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

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

类别

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

说明

0/200

提交
取消

辅 助

模 式