5

我用于 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();
}

}

4

3 回答 3

7

每次添加新的子节点时,请确保扩展从新节点的父节点到根节点的路径:

DefaultMutableTreeNode newChild = new DefaultMutableTreeNode("Node " + (root.getChildCount() + 1));
dm.insertNodeInto(newChild, root, root.getChildCount());
tree.expandPath(new TreePath(dm.getPathToRoot(newChild.getParent())));
于 2012-10-13T20:13:06.627 回答
3

Please also check another solution:

   DefaultTreeModel model = (DefaultTreeModel) (tree.getModel()); 
   model.reload(); 

API says:

invoke reload method if you've modified the TreeNodes upon which this model depends. The model will notify all of its listeners that the model has changed.

For a quick verification try the following:

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());
   }
}

Please see the following answer for more details:

于 2012-10-13T21:01:46.907 回答
3

@JBNizet 的答案是正确的,总结一下:

  • 插入节点不会自动影响父节点的展开状态
  • 所以为了确保新插入的孩子是可见的,它的父母必须展开

加入一些替代 api 以使孩子可见:

dm.makeVisible(new TreePath(newChild.getPath());

关于@Geoff Reedy 的评论(由我加粗):

JTree 的根节点通常是展开的,但是当树创建时,根节点没有任何子节点,因此无法展开

cannot没有那么严格:它是否可以扩展(即使有零个孩子)实际上取决于模型的 isLeaf 的实现。可以说,根永远不会是叶子(即使没有孩子),因为它的本质是不是 :-) 可以通过以下方式实现

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();
    }

};

通过这样的实现,根总是被扩展(根可见或不可见)并且新插入的直接子将显示而不使它们显式可见。

于 2012-10-17T16:12:18.507 回答