如何知道Swing JTree何时进行了所有更改?

时间:2016-06-19 12:40:03

标签: java swing jtree

所以我修改了我的树结构并要求我的树模型重新加载它。如何知道JTree组件何时完成呈现更改?

1 个答案:

答案 0 :(得分:2)

以下Oracle文档部分涵盖了这一点:

通过添加自己的侦听器,您可以监视更改。但是,DefaultTreeModel在通知更改时从最后到第一个处理侦听器(来源:DefaultTreeModel):

// Process the listeners last to first, notifying those that are interested in this event

因此,为了确保在之后通知您的监听器已经呈现/完成了更改,您需要在JTree内部已经在内部使用的监听器之后执行监听器。处理变化。通过扩展DefaultTreeModel并覆盖这些方法,您可以通过修改for循环/引入排序来更改侦听器的处理顺序

上面链接的教程中的DynamicTreeDemoDynamicTree为自己进行测试提供了很好的基础,因为它们提供了编辑,插入和删除树节点的方法,以及完全修改树结构(明确的选项)通过用户界面。每个对应于TreeModelListener中必须实现的方法

以下是您可以通过包含

进行测试的示例
treeModel = new CustomTreeModel(rootNode);
treeModel.addTreeModelListener(new CustomTreeModelListener());
DynamicTree构造函数中的

您还可以添加其他TreeModelListener以确保按预期顺序执行

型号:

class CustomTreeModel extends DefaultTreeModel{
        public CustomTreeModel(TreeNode root){
            super(root);
        }

        @Override
        protected void fireTreeNodesChanged(Object source, Object[] path,
                                            int[] childIndices, Object[] children) {

            Object[] listeners = listenerList.getListenerList();
            TreeModelEvent event = null;
            // Process the listeners first to last, notifying those that are interested in this event
            for (int index = 0; index < listeners.length; index++) {
                if (listeners[index] instanceof TreeModelListener) {
                    // Lazily create the event:
                    if (event == null)
                        event = new TreeModelEvent(source, path, childIndices, children);
                    ((TreeModelListener)listeners[index]).treeNodesChanged(event);
                }
            }
        }

        @Override
        protected void fireTreeNodesInserted(Object source, Object[] path,
                                             int[] childIndices, Object[] children) {

            Object[] listeners = listenerList.getListenerList();
            TreeModelEvent event = null;
            // Process the listeners first to last, notifying those that are interested in this event
            for (int index = 0; index < listeners.length; index++) {
                if (listeners[index] instanceof TreeModelListener) {
                    // Lazily create the event:
                    if (event == null)
                        event = new TreeModelEvent(source, path, childIndices, children);
                    ((TreeModelListener)listeners[index]).treeNodesInserted(event);
                }
            }
        }

        @Override
        protected void fireTreeNodesRemoved(Object source, Object[] path,
                                            int[] childIndices, Object[] children) {

            Object[] listeners = listenerList.getListenerList();
            TreeModelEvent event = null;
            // Process the listeners first to last, notifying those that are interested in this event
            for (int index = 0; index < listeners.length; index++) {
                if (listeners[index] instanceof TreeModelListener) {
                    // Lazily create the event:
                    if (event == null)
                        event = new TreeModelEvent(source, path, childIndices, children);
                    ((TreeModelListener)listeners[index]).treeNodesRemoved(event);
                }
            }
        }

        @Override
        protected void fireTreeStructureChanged(Object source, Object[] path,
                                                int[] childIndices, Object[] children) {

            Object[] listeners = listenerList.getListenerList();
            TreeModelEvent event = null;
            // Process the listeners first to last, notifying those that are interested in this event
            for (int index = 0; index < listeners.length; index++) {
                if (listeners[index] instanceof TreeModelListener) {
                    // Lazily create the event:
                    if (event == null)
                        event = new TreeModelEvent(source, path, childIndices, children);
                    ((TreeModelListener)listeners[index]).treeStructureChanged(event);
                }
            }
        }
    }

监听器:

class CustomTreeModelListener implements TreeModelListener {
    public void treeNodesChanged(TreeModelEvent e) {
        DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode)e.getTreePath().getLastPathComponent();
        int index = e.getChildIndices()[0];
        DefaultMutableTreeNode modifiedNode = (DefaultMutableTreeNode)(parentNode.getChildAt(index));

        System.out.println("A node on parent: " + parentNode.getUserObject()
                + " was modified to: " + modifiedNode.getUserObject());
    }

    public void treeNodesInserted(TreeModelEvent e) {
        String parentNode = e.getTreePath().getLastPathComponent().toString();
        String childNodes = Arrays.toString(e.getChildren());
        System.out.println("Node(s): " + childNodes + " inserted to parent: " + parentNode);
    }

    public void treeNodesRemoved(TreeModelEvent e) {
        String parentNode = e.getTreePath().getLastPathComponent().toString();
        String childNodes = Arrays.toString(e.getChildren());
        System.out.println("Node(s): " + childNodes + " removed from parent: " + parentNode);
    }

    public void treeStructureChanged(TreeModelEvent e) {
        System.out.println("Tree structure has been updated");
    }
}