updating JTree in java GUI

前端 未结 3 1812
失恋的感觉
失恋的感觉 2021-02-14 08:03

I used a JTree in my GUI and added it to a JFrame. When I want to update it and change it\'s nodes in another part of my program (while program is running, as an action performe

相关标签:
3条回答
  • 2021-02-14 08:25

    You need to ensure that after updating your model you instruct it to fire an event to cause any registered listeners to be notified of the event. One of the listeners will be the JTree and upon receiving the event it will repaint.

    For example, DefaultTreeModel contains the methods:

    nodeChanged nodesChanged nodeStructureChanged nodesWereInserted nodesWereRemoved

    Also, as with all Swing programming you need to ensure you are updating your model on the Event Dispatch Thread.

    0 讨论(0)
  • 2021-02-14 08:38

    In addition to the insertNodeInto suggestion you can also use:

    DefaultTreeModel model = (DefaultTreeModel)tree.getModel();
    DefaultMutableTreeNode root = (DefaultMutableTreeNode)model.getRoot();
    root.add(new DefaultMutableTreeNode("another_child"));
    model.reload(root);
    
    0 讨论(0)
  • 2021-02-14 08:41

    Do you mean the GUI aspect just isn't showing your change? You should probably look int repaint() and revalidate().

    Here's a good description of when to call which one.

    0 讨论(0)
提交回复
热议问题