简体   繁体   中英

Update child (panel) of CardLayout

My main JFrame content panel which is card layout and I have added many (say panel1 , panel2 ) panel as card. I would like to update UI panel1 when something changes in panel2 .

How to update child (panel) of CardLayout in Swing?

The observer pattern , discussed here , is the key to this. In particular, both panels could listen to a common model, which would fire events to update each listening panel. Examples using PropertyChangeListener may be found here and here .

Maintain a reference to the target of what you want to change, and use some kind of callback function to detect the changes. Use the stored reference in the callback function. How you specifically implement this should be whatever makes most sense for your code... but what you are trying to do is a simple task.

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