Giannis
Giannis

Reputation: 5526

How to keep a swing component updated

One of my classes is returning a JPanel which is added on a JFrame by some other class. The JPanel contains a JTree and some buttons. On some events the panel is created again and returned to the JFrame.

My problem is that I have to add the JPanel to the Container of the JFrame again and then resize the frame for changes to be visible. I can't figure how to have the frame update without resizing.I tried removing old objects and adding updated ones but still doesn't work.

What is the best way to deal with this issue? Ideally I would have a reference to the JPanel and when the JPanel is changed, the frame will also be updated.

Upvotes: 0

Views: 249

Answers (2)

camickr
camickr

Reputation: 324118

The whole model is changing not just its data. I will probably change this in the future but for now when data change a new JTree is created

Then your code should be something like:

JTree tree = new JTree( theNewModel );
scrollPane.setViewportView( tree );

That is you need to add the new JTree to the GUI, you can't just change the reference to the tree variable.

Or even easier, you don't need to create a new JTree, just replace the model in the existing tree using:

tree.setModel( theNewModel );

If this still doesn't help then you need to post your SSCCE that demonstrates the problem because your question still isn't clear.

Upvotes: 3

caymayan dalgaci
caymayan dalgaci

Reputation: 1

try JFrame.invalidate() first, then call JFrame.validate()

http://docs.oracle.com/javase/6/docs/api/java/awt/Container.html#invalidate%28%29

Upvotes: -2

Related Questions