我想起了这个SSCCE来重现我在一个更大的程序中遇到的一些奇怪的行为:
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;
public class TreeExpandTest {
private JFrame frame;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
TreeExpandTest window = new TreeExpandTest();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public TreeExpandTest() {
initialize();
}
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
final JTree tree = new JTree(root);
tree.setShowsRootHandles(true);
final DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
final DefaultMutableTreeNode level1 = new DefaultMutableTreeNode("Level 1");
root.add(level1);
DefaultMutableTreeNode level2a = new DefaultMutableTreeNode("Level 2a");
level1.add(level2a);
final TreeNode[] nodes = model.getPathToRoot(level1);
tree.expandPath(new TreePath(nodes));
frame.getContentPane().add(tree, BorderLayout.CENTER);
JButton btnDoStuff = new JButton("Remove 2a, replace with 2b");
frame.getContentPane().add(btnDoStuff, BorderLayout.SOUTH);
btnDoStuff.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
level1.remove(0);
int[] removed = {0};
model.nodesWereRemoved(level1, removed, null);
DefaultMutableTreeNode level2b = new DefaultMutableTreeNode("Level 2b");
level1.insert(level2b, 0);
int[] inserted = {0};
model.nodesWereInserted(level1, inserted);
//tree.collapsePath(new TreePath(nodes));
tree.expandPath(new TreePath(nodes));
}
});
}
}
要完全了解问题,请按以下步骤操作:
nodesWereInserted
方法,这就是我能做的所有,对吧?我找到了一个“解决”问题的解决方法,但不满足我的好奇心。
问题仍然存在......出了什么问题,为什么需要我的解决方法?为什么在告诉它之前我需要折叠树我希望它被扩展?
编辑添加:只有在我删除/替换唯一的孩子时才会出现这种情况。如果有多个孩子,我删除/替换父母永远不会没有孩子,那么每件事都会按照预期行事。
答案 0 :(得分:4)
通知不正确,必须是:
level1.remove(0);
int[] removed = {0};
Object[] children = new Object[]{level2a};
model.nodesWereRemoved(level1, removed, children);
注意子数组而不是null
顺便说一下,我建议尽可能使用模型api修改节点/结构(而不是改变其脚下的节点)。对于单节点更改,上面将是:
model.removeNodeFromParent(level2a);
DefaultMutableTreeNode level2b = new DefaultMutableTreeNode("Level 2b");
model.insertNodeInto(level2b, level1, 0);