DefaultMutableTreeNode-Text is too long?

喜欢而已 提交于 2019-11-29 17:31:22

the underlying reason is that the layout of the tree nodes is cached and the cache not updated properly. Might f.i. happen if the node is changed under feet of the model, uncomment the nodeChanged to see the difference

    JTree tree = new JTree();
    DefaultMutableTreeNode root = (DefaultMutableTreeNode) tree.getModel().getRoot();
    int index = 0;
    DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
    String result = "\n";
    Enumeration<?> enumer = root.preorderEnumeration();
    while (enumer.hasMoreElements()) {
        DefaultMutableTreeNode node = (DefaultMutableTreeNode) enumer.nextElement();
        String nodeValue = String.valueOf(node.getUserObject());
        node.setUserObject(nodeValue + ": " + index++);
       //model.nodeChanged(node);
    }

The exact reason in your context might vary, no way to tell without an sscce

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!