Skip to content
Advertisement

How to Handle Events in a multiple Class Java Swing Project?

For example I have two Java Classes:

JavaScript

What can i do to make a feature that when i click a button from the SouthPanel and foo in MainFrame will change.

Advertisement

Answer

So, in most UI frameworks, you have a concept of an “observer pattern”, this is way for interested parties to register interest been notified when something happens to the object.

Swing makes use of “listeners” to facilitate this basic concept. You’re not stuck to using the listeners that Swing defines and you can create your own, for example…

JavaScript

This example is deliberately long winded, as you don’t “always” need to create an event object, but it is a good way to pass back information to the observer, as it might not actually have a direct reference to object it’s monitoring.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement