I looked for answer but wasn't able find anything to solve this. I have two array lists containing Strings that're changing dynamically after user click, and now creating JTree that will show elements from one list depends on some type in another list. I have a problem because I always add and delete something in my list and need JTree to show that changes. It will add all elements from List correctly till I expand, after that it's impossible to update content of JTree, at least for me. If I empty my list it will show like it's collapsed but all content is still showing like it's expanded even it gives in console that childcount is 0, and if I add something to my list it's going to add it but won't show it. So basically the problem is to visually show changes of JTree.
public class JSelectedTree extends JPanel{
private JTree selectionTree;
private DefaultTreeModel treeModel;
public DefaultMutableTreeNode point;
public DefaultMutableTreeNode polygon;
public DefaultMutableTreeNode line;
public JSelectedTree() {
initGUI();
}
public void initGUI() {
DefaultMutableTreeNode root = new DefaultMutableTreeNode("root");
point = new DefaultMutableTreeNode("Point");
polygon = new DefaultMutableTreeNode("Polygon");
line = new DefaultMutableTreeNode("Line");
root.add(point);
root.add(polygon);
root.add(line);
treeModel = new DefaultTreeModel(root);
selectionTree = new JTree(root);
selectionTree.setRootVisible(false);
selectionTree.setShowsRootHandles(false);
selectionTree.getSelectionModel().setSelectionMode(
TreeSelectionModel.SINGLE_TREE_SELECTION);
JScrollPane scroll = new JScrollPane(selectionTree);
scroll.setBorder(BorderFactory.createTitledBorder("Selected"));
setLayout(new BorderLayout());
add(scroll, BorderLayout.CENTER);
}
public void resetTree(){
if(!MultiSelectTool.selectedObject.isEmpty()){
polygon.removeAllChildren();
point.removeAllChildren();
line.removeAllChildren();
treeModel.reload();
int n = MultiSelectTool.selectedObject.size();
for(int i = 0; i < n; i++){
if(MultiSelectTool.selectedCoords.get(i).contains("(((")){
DefaultMutableTreeNode child = new DefaultMutableTreeNode(MultiSelectTool.selectedObject.get(i));
treeModel.insertNodeInto(child, polygon, polygon.getChildCount());
}else if(MultiSelectTool.selectedCoords.get(i).contains("((")){
DefaultMutableTreeNode child = new DefaultMutableTreeNode(MultiSelectTool.selectedObject.get(i));
treeModel.insertNodeInto(child, line, line.getChildCount());
}else{
DefaultMutableTreeNode child = new DefaultMutableTreeNode(MultiSelectTool.selectedObject.get(i));
treeModel.insertNodeInto(child, point, point.getChildCount());
}
}
}else{
polygon.removeAllChildren();
point.removeAllChildren();
line.removeAllChildren();
treeModel.reload();
}
}
}