2
tree.addTreeExpansionListener(new TreeExpansionListener() {
@Override
public void treeExpanded(TreeExpansionEvent event) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode) event
.getPath().getLastPathComponent();
node.removeAllChildren();
//System.out.println(node.getChildCount());
child = new DefaultMutableTreeNode("new node");
node.add(child);;
frame.doLayout();
frame.repaint();
}
@Override
public void treeCollapsed(TreeExpansionEvent event) {
// TODO Auto-generated method stub
}
});
Вы должны уведомить 'TreeModel', что-то вроде [' JTree # insertNodeInto'] (HTTP: // документы .oracle.com/javase/7/docs/api/javax/swing/tree/DefaultTreeModel.html # insertNodeInto (javax.swing.tree.MutableTreeNode,% 20javax.swing.tree.MutableTreeNode,% 20int)) и ['TreeModel # removeNodeFromParent'] (http://docs.oracle.com/javase/7/docs/api/javax/swing/tree/DefaultTreeModel.html#removeNodeFromParent (javax.swing.tree.MutableTreeNode)) для [example] (http://stackoverflow.com/questions/22683092/jtree-avoid-collapse-after-reload/22683989#22683989) – MadProgrammer
См. графический интерфейс [File Browser GUI ] (http://codereview.stackexchange.com/q/4446/7784) для примера. –