为什么JTree 初始时根节点是展开的? 你的JTree怎么初始化的?代码贴出来使用DefaultMutableTreeNode和DefaultTreeModel了吗? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 这段代码原来是 Core Java 作者 Cay Horstmann 写的我给他加了一点点,就行了构造函数里,最后一句,tree.collapsePath(new TreePath(root));/** * @version 1.00 1999-07-17 * @author Cay Horstmann */import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.tree.*;public class TreeEditTest{ public static void main(String[] args) { JFrame frame = new TreeEditFrame(); frame.show(); }}class TreeEditFrame extends JFrame implements ActionListener{ public TreeEditFrame() { setTitle("TreeEditTest"); setSize(300, 200); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } } ); // construct tree TreeNode root = makeSampleTree(); model = new DefaultTreeModel(root); tree = new JTree(model); tree.setEditable(true); // add scroll pane with tree to content pane Container contentPane = getContentPane(); JScrollPane scrollPane = new JScrollPane(tree); contentPane.add(scrollPane, "Center"); // make button panel JPanel panel = new JPanel(); addSiblingButton = new JButton("Add Sibling"); addSiblingButton.addActionListener(this); panel.add(addSiblingButton); addChildButton = new JButton("Add Child"); addChildButton.addActionListener(this); panel.add(addChildButton); deleteButton = new JButton("Delete"); deleteButton.addActionListener(this); panel.add(deleteButton); contentPane.add(panel, "South"); tree.collapsePath(new TreePath(root)); } public TreeNode makeSampleTree() { DefaultMutableTreeNode root = new DefaultMutableTreeNode("World"); DefaultMutableTreeNode country = new DefaultMutableTreeNode("USA"); root.add(country); DefaultMutableTreeNode state = new DefaultMutableTreeNode("California"); country.add(state); DefaultMutableTreeNode city = new DefaultMutableTreeNode("San Jose"); state.add(city); city = new DefaultMutableTreeNode("Cupertino"); state.add(city); state = new DefaultMutableTreeNode("Michigan"); country.add(state); city = new DefaultMutableTreeNode("Ann Arbor"); state.add(city); country = new DefaultMutableTreeNode("Germany"); root.add(country); state = new DefaultMutableTreeNode("Schleswig-Holstein"); country.add(state); city = new DefaultMutableTreeNode("Kiel"); state.add(city); return root; } public void actionPerformed(ActionEvent event) { DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent(); if (selectedNode == null) return; if (event.getSource().equals(deleteButton)) { if (selectedNode.getParent() != null) model.removeNodeFromParent(selectedNode); return; } // add new node as sibling or child DefaultMutableTreeNode newNode = new DefaultMutableTreeNode("New"); if (event.getSource().equals(addSiblingButton)) { DefaultMutableTreeNode parent = (DefaultMutableTreeNode)selectedNode.getParent(); if (parent != null) { int selectedIndex = parent.getIndex(selectedNode); model.insertNodeInto(newNode, parent, selectedIndex + 1); } } else if (event.getSource().equals(addChildButton)) { model.insertNodeInto(newNode, selectedNode, selectedNode.getChildCount()); } // now display new node TreeNode[] nodes = model.getPathToRoot(newNode); TreePath path = new TreePath(nodes); tree.scrollPathToVisible(path); } private DefaultTreeModel model; private JTree tree; private JButton addSiblingButton; private JButton addChildButton; private JButton deleteButton; private JButton editButton;} 谢谢 DanielYWoo(绿色毒汁):你的方法是可以的。我的思路是这样的:JTree里面的数据是动态生成的(从xml种读出来),点一个节点就去找读取它的儿子,再展开。你的方法等于它展开后又把她合上了,有点欺骗用户的味道,呵呵不知道有没有别的方法?谢谢 锷,原来这个意思你可以在树模型监听器里 void treeNodesChanged(TreeModelEvent e) 方法判断如果新选定的节点,没有子节点,则去读,如果能读到,他在XML里不是叶结点,就读入并expand开他的子节点,否则这可能是叶结点,就不用读了如果新选定的节点,有子节点,那时已经被读过了,则不去读啦 你可以用collapsePath(treePath)和expandPath(treePah)在程序中去根据需要展开或者关闭节点。 请老师们 看看 我这里哪里出现问题 ?谢谢。 Worldwind Java调用WMS服务的问题 大家说说为什么项目中不直接把java编译成二进制native code?不是就快了么 誰能提供下JCL相關資料? 菜单栏位置不对,网格布局无效 JDBC事务处理问题 如果实现鼠标双键事件? 有关jni的使用,急 请问Java如何得到本地机器名字? 请教有没有考虑编程性能的资料,紧急 请问java中乘法是怎么实现的? 怎么散分
我给他加了一点点,就行了
构造函数里,最后一句,tree.collapsePath(new TreePath(root));/**
* @version 1.00 1999-07-17
* @author Cay Horstmann
*/import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.tree.*;public class TreeEditTest
{ public static void main(String[] args)
{ JFrame frame = new TreeEditFrame();
frame.show();
}
}class TreeEditFrame extends JFrame
implements ActionListener
{ public TreeEditFrame()
{ setTitle("TreeEditTest");
setSize(300, 200);
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{ System.exit(0);
}
} ); // construct tree TreeNode root = makeSampleTree();
model = new DefaultTreeModel(root);
tree = new JTree(model);
tree.setEditable(true); // add scroll pane with tree to content pane Container contentPane = getContentPane();
JScrollPane scrollPane = new JScrollPane(tree);
contentPane.add(scrollPane, "Center"); // make button panel JPanel panel = new JPanel();
addSiblingButton = new JButton("Add Sibling");
addSiblingButton.addActionListener(this);
panel.add(addSiblingButton);
addChildButton = new JButton("Add Child");
addChildButton.addActionListener(this);
panel.add(addChildButton);
deleteButton = new JButton("Delete");
deleteButton.addActionListener(this);
panel.add(deleteButton);
contentPane.add(panel, "South");
tree.collapsePath(new TreePath(root));
} public TreeNode makeSampleTree()
{ DefaultMutableTreeNode root
= new DefaultMutableTreeNode("World");
DefaultMutableTreeNode country
= new DefaultMutableTreeNode("USA");
root.add(country);
DefaultMutableTreeNode state
= new DefaultMutableTreeNode("California");
country.add(state);
DefaultMutableTreeNode city
= new DefaultMutableTreeNode("San Jose");
state.add(city);
city = new DefaultMutableTreeNode("Cupertino");
state.add(city);
state = new DefaultMutableTreeNode("Michigan");
country.add(state);
city = new DefaultMutableTreeNode("Ann Arbor");
state.add(city);
country = new DefaultMutableTreeNode("Germany");
root.add(country);
state = new DefaultMutableTreeNode("Schleswig-Holstein");
country.add(state);
city = new DefaultMutableTreeNode("Kiel");
state.add(city);
return root;
} public void actionPerformed(ActionEvent event)
{ DefaultMutableTreeNode selectedNode
= (DefaultMutableTreeNode)
tree.getLastSelectedPathComponent(); if (selectedNode == null) return; if (event.getSource().equals(deleteButton))
{ if (selectedNode.getParent() != null)
model.removeNodeFromParent(selectedNode);
return;
} // add new node as sibling or child DefaultMutableTreeNode newNode
= new DefaultMutableTreeNode("New"); if (event.getSource().equals(addSiblingButton))
{ DefaultMutableTreeNode parent
= (DefaultMutableTreeNode)selectedNode.getParent(); if (parent != null)
{ int selectedIndex = parent.getIndex(selectedNode);
model.insertNodeInto(newNode, parent,
selectedIndex + 1);
}
}
else if (event.getSource().equals(addChildButton))
{ model.insertNodeInto(newNode, selectedNode,
selectedNode.getChildCount());
} // now display new node TreeNode[] nodes = model.getPathToRoot(newNode);
TreePath path = new TreePath(nodes);
tree.scrollPathToVisible(path);
} private DefaultTreeModel model;
private JTree tree;
private JButton addSiblingButton;
private JButton addChildButton;
private JButton deleteButton;
private JButton editButton;
}
你可以在树模型监听器里 void treeNodesChanged(TreeModelEvent e) 方法
判断
如果新选定的节点,没有子节点,则去读,如果能读到,他在XML里不是叶结点,就读入并expand开他的子节点,否则这可能是叶结点,就不用读了
如果新选定的节点,有子节点,那时已经被读过了,则不去读啦
collapsePath(treePath)
和expandPath(treePah)
在程序中去根据需要展开或者关闭节点。