所以我修改了我的树结构并要求我的树模型重新加载它。如何知道JTree组件何时完成呈现更改?
答案 0 :(得分:2)
以下Oracle文档部分涵盖了这一点:
通过添加自己的侦听器,您可以监视更改。但是,DefaultTreeModel
在通知更改时从最后到第一个处理侦听器(来源:DefaultTreeModel):
// Process the listeners last to first, notifying those that are interested in this event
因此,为了确保在之后通知您的监听器已经呈现/完成了更改,您需要在JTree
内部已经在内部使用的监听器之后执行监听器。处理变化。通过扩展DefaultTreeModel
并覆盖这些方法,您可以通过修改for循环/引入排序来更改侦听器的处理顺序
上面链接的教程中的DynamicTreeDemo
和DynamicTree
为自己进行测试提供了很好的基础,因为它们提供了编辑,插入和删除树节点的方法,以及完全修改树结构(明确的选项)通过用户界面。每个对应于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");
}
}