Ben
Ben

Reputation: 7

Java GUI - Changing inside JPanels dynamically

Trying to figure out if it's possible and difficultly level around the below problem as depending on such might consider other alternatives...

If I have a overall JFrame framework, can I construct various different JPanels with their associated components and actions then say pass these JPanels as args depending on user interaction so the inside of the overall JFrame/JPanel changes. I'm assuming there must be some implementation that achieves this, but having trouble find the answer.. For example I construct a JPanel, which has border layout, and the centre position will change different JPanels depending on what a user does etc.. I thought it would be a simple as create a JPanel, then passing it to a method which calls the overall Jpanel add(component, borderlayout.center) method which would change what is shown, but doesn't work like that and assumed that must only work for constructor when GUI is first constructed..

Sorry for the length, but if someone can point me in the right direction i'd be appreciated...

Upvotes: 0

Views: 766

Answers (1)

JB Nizet
JB Nizet

Reputation: 692131

Removing and adding components does work as expected. You need to call revalidate() on the parent component once it's done, though.

If that doesn't work, post an SSCCE exhibiting the problem.

Upvotes: 2

Related Questions