我在JDialog中使用的JTree节点可见性存在问题。 当我想向模型添加新节点时,Jtree不会刷新。
奇怪的是节点正在更新,就像我设置setRootVisible(true)一样。
这是代码。提前致谢
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
public class TestClass {
JTree tree;
DefaultTreeModel dm;
JDialog dialog;
public TestClass(){
JFrame frame = new JFrame("title");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
JPanel showPane = new JPanel();
showPane.setLayout(new BorderLayout());
dm = new DefaultTreeModel(new DefaultMutableTreeNode("root"));
tree = new JTree(dm);
tree.setRootVisible(false);
JButton button = new JButton("add node");
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
DefaultMutableTreeNode root = (DefaultMutableTreeNode) tree.getModel().getRoot();
dm.insertNodeInto(new DefaultMutableTreeNode("Node " + (root.getChildCount() + 1)), root, root.getChildCount());
int c = root.getChildCount();
System.out.println("child count: " + c);
for(int i=0; i<c; i++){
DefaultMutableTreeNode node = (DefaultMutableTreeNode) root.getChildAt(i);
System.out.println("has node:" + node.getUserObject().toString());
}
}
});
showPane.add(tree, BorderLayout.CENTER);
showPane.add(button, BorderLayout.PAGE_END);
JComponent[] inputComponents = new JComponent[] {showPane};
Object[] opButtons = {"OK"};
JOptionPane optPane = new JOptionPane(inputComponents
, JOptionPane.PLAIN_MESSAGE
, JOptionPane.CLOSED_OPTION
, null
, opButtons
, opButtons[0]);
optPane.setPreferredSize(new Dimension(400 ,250));
dialog = optPane.createDialog(null, "Create new Application Node");
dialog.setLocationRelativeTo(frame);
dialog.setVisible(true);
if(optPane.getValue() != null){
System.exit(0);
}
}
public static void main(String arg[]){
TestClass myClass = new TestClass();
}
}
答案 0 :(得分:7)
每次添加新的子节点时,请确保将新节点的父节点的路径扩展到根目录:
DefaultMutableTreeNode newChild = new DefaultMutableTreeNode("Node " + (root.getChildCount() + 1));
dm.insertNodeInto(newChild, root, root.getChildCount());
tree.expandPath(new TreePath(dm.getPathToRoot(newChild.getParent())));
答案 1 :(得分:3)
还请检查另一种解决方案:
DefaultTreeModel model = (DefaultTreeModel) (tree.getModel());
model.reload();
API说:
如果你修改了TreeNodes,那么调用reload方法 模特取决于。该模型将通知其所有听众 模特已经改变了。
要快速验证,请尝试以下操作:
public void actionPerformed(ActionEvent arg0) {
DefaultMutableTreeNode root = (DefaultMutableTreeNode) tree.getModel().getRoot();
dm.insertNodeInto(new DefaultMutableTreeNode("Node " + (root.getChildCount() + 1)), root, root.getChildCount());
int c = root.getChildCount();
System.out.println("child count: " + c);
DefaultTreeModel model = (DefaultTreeModel) (tree.getModel()); // !!!
model.reload(); // !!!
for (int i = 0; i < c; i++) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode) root.getChildAt(i);
System.out.println("has node:" + node.getUserObject().toString());
}
}
有关详细信息,请参阅以下答案:
答案 2 :(得分:3)
@JBNizet的答案是正确的,总结一下:
要投入一些替代api以使孩子可见:
dm.makeVisible(new TreePath(newChild.getPath());
关于@Geoff Reedy的评论(由我加冕):
不能不是那么严格:它是否可以扩展(即使是零孩子)实际上取决于模型的实现isLeaf。可以说,根永远不是一片叶子(即使没有孩子)因为它的本质不是:-)可以实现通常会扩展JTree的根节点,但是在创建树时,根节点没有任何子节点,因此无法展开
final DefaultTreeModel model = new DefaultTreeModel(root) {
@Override
public boolean isLeaf(Object node) {
if (isRoot(node)) {
return false;
}
return super.isLeaf(node);
}
private boolean isRoot(Object node) {
return node != null && node == getRoot();
}
};
通过这样的实现,根目录总是被扩展(root可见或不可见),并且新插入的直接子节点将显示而不会使它们明确可见。