Reputation: 30086
I've developed a simple Swing GUI to allow the user to edit the data of my application. The UI is rather simple:
But there's a problem: I need to sync the different JPanels.
For example, the data object stores a list of persons and a list of tasks.
One JPanel allows the user to edit the persons. He can create new Persons and delete existing ones.
The second JPanel allows the user to assign persons to tasks.
Both JPanels are initialized when they are first opened. The components are created and the layout is set. Now the user can start to work with the UI.
But the second tab and the JPanel it contains have already been initialized. The user will not see the second person, unless the JPanel is updated. I have implemented a custom update method on my JPanel subclass for tab 2. But how do I call it ?
I need the Swing event that is fired on a Component, when it is redrawn.
Upvotes: 0
Views: 271
Reputation: 636
Actually updating the second panel when the first gets redrawn is bad idea even if you manage to find the event that gets fired. This would cause the second panel to be updated on every redraw of the first panel, regardless of wether the redraw means that the undelying data has changed or not (think about unsaved data).
You should make the data your tabs maipulate Observable
(or Subject
from Observer Pattern). Then each tab can react to changes in the data instead of changes in other tabs. This will reduce the time of updates to only when it is really needed and also will decouple the tabs from one another.
The data may repesent changes by firing PropertyChangeEvent's. These in turn should trigger Controller actions as suggested by Gilber Le Blanc in the comments.
Upvotes: 1