2

I am trying to remove node from JTree and i have two codes That code is working

public class test {
private JFrame frame;
JTree tree;
DefaultMutableTreeNode root;
DefaultTreeModel model;
private Map<String, DefaultMutableTreeNode> treeMap;

public static void main(String[] args) {
    test t = new test();
    //scrolltree is basically a panel contains class has JTree
    scrolltree st = new scrolltree();
    t.frame.add(st);
    JButton b = new JButton("remove node");
    t.frame.add(b, BorderLayout.SOUTH);
    b.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            DefaultTreeModel model = (DefaultTreeModel) st.getTree().getTree().getModel();
            String key = new currentfolder().getCurrentfolder() + "/" + "a3.png";
            System.out.println(key);
            model.removeNodeFromParent(st.getTree().getTreeMap().get(key));
        }
    });
    t.frame.revalidate();
    t.frame.repaint();

}

public test() {
    frame = new JFrame();
    frame.setVisible(true);
    frame.setSize(500, 500);
}

and that code removes it perfectly and updated it too

while the other same one delete the node but doesn`t update it(unless i created new tree and replaced with old one)

i have tried all listed solution in many placed like https://community.oracle.com/thread/2495518?tstart=0

but non of them work my non-working code goes as follows

     DefaultTreeModel model = (DefaultTreeModel)startframe.getScrolltree().getTree().getTree().getModel();
     String key = new currentfolder().getCurrentfolder()+"/"+"a3.png";
     System.out.println(key);
     model.removeNodeFromParent(startframe.getScrolltree().getTree().getTreeMap().get(key));

and my class tree goes as following

public class tree {
JTree tree;
DefaultMutableTreeNode node1;
DefaultTreeModel model;
String name = new currentfolder().getInitial();// path to directory that its
                                                // content appear in tree
private Map<String, DefaultMutableTreeNode> treeMap;

public tree() {
    tree = new JTree();
    tree.setExpandsSelectedPaths(true);
    node1 = new DefaultMutableTreeNode(name);
    model = new DefaultTreeModel(node1);
    tree.setModel(model);
    treeMap = new HashMap<>();
    treeMap.put(name, node1);
    //method to inset many other nodes
    import_data(new File(name), node1);

    model.reload();
    tree.setOpaque(false);
    tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
    tree.setBorder(javax.swing.BorderFactory.createCompoundBorder(
            new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED),
            javax.swing.BorderFactory.createTitledBorder("")));
    tree.setVisible(true);

when i do tree t = new tree; then place it over old tree it works

how can I fix that and any explanation why this isn`t working? thanks

Kareem Elsayed
  • 295
  • 6
  • 22
  • @trashgod can you check this question ? – Kareem Elsayed Jun 23 '16 at 12:54
  • First, be sure to construct and manipulate Swing GUI objects _only_ on the [event dispatch thread](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html). – trashgod Jun 23 '16 at 18:25
  • for better help sooner post an SSCCE / MCVE, short, runnable, compilable, by using Icons from UIManager, mabye something inside model.removeNodeFromParent(startframe.getScrolltree().getTree().getTreeMap().get(key)); doens't works coorectly (meaning notifiers in JTree / treemodel ....), just guessing – mKorbel Jun 24 '16 at 07:40
  • whats reasons for t.frame.revalidate(); and t.frame.repaint(); (more is explained in comment by @trashgod) – mKorbel Jun 24 '16 at 07:41
  • @mKorbel its just for updating after adding new swing components,yes i better be using swing utilites,thanks for this point – Kareem Elsayed Jun 24 '16 at 10:27
  • @trashgod i am already using swingutilites with the non working code – Kareem Elsayed Jun 24 '16 at 10:29
  • @mKorbel i though this method is the best way to remove node as it notify all required events to update gui and jtree too ?right ? – Kareem Elsayed Jun 24 '16 at 11:18
  • @mKorbel is right; without a [mcve], we're just guessing. – trashgod Jun 24 '16 at 15:25
  • @trashgod I figured out the problem .i was using thread and in its runnable i created `SwingUtilites.invokeAndWait()`........when I replace all that with `SwingUtilites.InvokeLater` it worked.i dont understand why as it supposed they both do the same job except for waiting part as descriped in your link too – Kareem Elsayed Jun 24 '16 at 16:12
  • post it as a solution so i can close the question. thank you. – Kareem Elsayed Jun 24 '16 at 16:14
  • @KareemElsayed: Kudos for persevering; I've elaborated below. – trashgod Jun 24 '16 at 19:25

1 Answers1

3

When I replace all that with SwingUtilites.invokeLater(), it worked. I don't understand why.

As suggested here, be sure to construct and manipulate Swing GUI objects only on the event dispatch thread‌​. In particular, updating the TreeModel from another thread means that the listening JTree view may receive updates in some unexpected order—or not at all.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045