How can I refresh a JTree after adding some nodes to the underlying model?

前端 未结 8 2107
情歌与酒
情歌与酒 2020-12-08 21:42

First of all, let me say that I dont use the DefaultTreeModel. I implement my own TreeModel, so i cant use the DefaultXXX stuff. The problem is this: Through some addStuff()

8条回答
  •  臣服心动
    2020-12-08 22:24

    Your TreeModel is supposed to fire TreeModelEvents when it changes, and the JTree observes your model though a TreeModelListener to refresh itself when your model changes.

    So if you implement the TreeModelListener support correctly, you do not need to observe the model and inform the JTree, as it already does so itself. From an MVC perspecive, the JTree is your View/Controller, and the TreeModel is your Model (or rather: model adapter), which is observable.

    You could try force the JTree to update its visual state by calling repaint(), but I would recommend not doing so as it's not guaranteed to work. When you're unsure of how to do a fine-granular notification to a TreeModelListener, use TreeModelListener.treeStructureChanged(..) to notify a 'whole model' update (warning: can cause selections and node expansion states to be lost).

提交回复
热议问题