开发者

Refresh Panel on CardLayout Swap

开发者 https://www.devze.com 2023-01-09 19:55 出处:网络
How do you refresh a JPanel on the开发者_Go百科 panel change from container CardLayout?Use the show() method. From the Java API:

How do you refresh a JPanel on the开发者_Go百科 panel change from container CardLayout?


Use the show() method. From the Java API:

Flips to the component that was added to this layout with the specified name, using addLayoutComponent. If no such component exists, then nothing happens.

CardLayout#first(), next(), and previous() do something similar.

Sometimes, when I've made a panel swap like this (though not that I can remember on CardLayout, but it's been a while since I used it), I've also needed to pack the frame again. If that doesn't work, you can call revalidate(), which lets Swing know that the component needs to be redrawn.

You may also want to consider using a tabbed pane, as it does a lot of this for you; I started out a project trying to use CardLayout and decided to use a the tabbed pane instead. Depends on what you want to do, of course.


Is there an actionlistener or something that I can have it reload/refresh my data on that screen?

Assuming you have a model that supplies data to your view (panel), two approaches are common:

  1. Arrange for your model to extend Observable and your view to register as an Observer.

  2. Arrange for your model to manage an EventListenerList, in effect creating you own analog of ActionEvent.

In either approach, use the listener of the control that switches views tell the model to update its registered observers/listeners. The example in How to Use CardLayout uses a JComboBox and itemStateChanged(). There's additional discussion here.


Is there an actionlistener or something that I can have it reload/refresh my data on that screen?

You can use an AncestorListener. It fires when a component is added to a Container. So when you swap cards the event is fired. You would need to add the listener to each panel you add to the CardLayout:

Another problem with the CardLayout is that the panel doesn't gain focus when it is swapped. I use this approach to set focus on the panel. Check out Card Layout Focus.

panel.addAncestorListener(...);


The show() method does the trick of switching panels, but it doesn't "refresh" the data. Is there an actionlistener or something that I can have it reload/refresh my data on that screen?

0

精彩评论

暂无评论...
验证码 换一张
取 消