3

I've got a JTree which I'm using to display some (unsurprisingly) hierarchical data. Part of the spec is that the user can change the data source (atm it's just between files). Now, when this happens, I can rebuild the data and the tree nodes with no problem. But, I'm having substantial difficulties getting the tree to update the changes. I tried removing it from it's scrollpane and replacing with a new JTree, but I didn't see any such. I've tried removing all from the JTree and didn't see any effect.

How can I make the JTree display changes after it's been constructed?
Ninjedit: Yes, I did call updateUI().

Another edit: I also wanted to replace the tree's current data with my new data. However, I don't see any methods that will take the DefaultMutableTreeNode that I constructed with. Even if I just remove the JTree and call updateUI on it's containing ScrollPane, nothing happens. Or if I use repaint instead.

4

1 に答える 1

5

適切なイベント (JTree 内部イベント) が発生していない可能性があります。たとえば、 を使用してノードを追加することもできnode.add(...)ますmodel.insertNodeInto(...)( を使用していると仮定するとDefaultTreeModel)。この場合、ビュー (JTree) を正しく更新する適切なイベントを起動するため、後者の方法が推奨されます。問題は UI の再描画にあるのではなく、実際にはモデルが変更されたことをビューに通知している可能性があります。
そのため、JTree を動的に変更する方法を調べることをお勧めします。可能であればDefaultTreeModel、ビューを駆動するモデルとして を使用することをお勧めします。

念のため、Sun JTree チュートリアルを読みましたか?

于 2010-08-11T16:23:00.163 に答える