belens
belens

Reputation: 962

Java Swing - CardLayout, how to 'update' cards while running?

I am pretty new at . Java Swing, and am using the CardLayout for my application. In this app a user can create activities and, in an other JPanel, view the existing ones.

I am having trouble updating the card (where the label is in). If I add the activity, when you restart the app it works fine. I am looking for a way that doesn't require restarting.

So in short, I would like to know how I should best reconstruct/update a panel whilst running so that the new label data is shown.

Thanks in advance!

Upvotes: 3

Views: 3521

Answers (2)

camickr
camickr

Reputation: 324128

When you add a component to a visible GUI you need to tell the GUI that a component has been added so the layout manager can be invoked:

panel.add( component );
panel.revalidate();

Upvotes: 8

arnehehe
arnehehe

Reputation: 1398

It sounds like what you are looking for is the Observer design pattern.

Basicly what it's all about is you have one Observer instance, which you add all your panels to. When you update a value in a panel, you nofity the Observer something's changed. The Observer will then inform all his observed panels that a value has changed, and they'll update their value.

http://en.wikipedia.org/wiki/Observer_pattern

Upvotes: 0

Related Questions