简体   繁体   中英

Refresh Panel on CardLayout Swap

如何从容器CardLayout刷新面板上的JPanel?

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 .

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?

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 technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM