3

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!

mKorbel
  • 109,525
  • 20
  • 134
  • 319
belens
  • 962
  • 1
  • 8
  • 18

2 Answers2

9

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();
camickr
  • 321,443
  • 19
  • 166
  • 288
  • Your answer fixed my problem!! thanks XD I've been struggling ages with this – belens Mar 12 '11 at 20:43
  • i have one panel which insert employee info. and another panel which display employee listing.which of both are in card layout. after inserting new employee and show next panel for employee listing but employee listing not refresing. any suggestion? – Krishna Shrestha Nov 24 '12 at 06:53
0

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

arnehehe
  • 1,386
  • 1
  • 17
  • 33