Bi-Directional Events Handling in Swing Apps (best practices)
Posted on 2014-03-12
I'm somewhat new to Java...
My current project is a controller app that connects to a piece of hardware via a socket connection.
I've made an an attempt to stick to the MVC model and added "Coms" (the socket connections etc) as a "subbranch" of "control".
The GUI is quite simple, a JFrame containing a JTabbedPane containing JPannels (and JTabbedPanes) and interactors (JButton, JTextFiled, JSlider).
The interactors on the GUI send out events to lookup some info in the "model" and then trigger messages sent to the hardware via the socket connection. To keep the connection between the UI and the Control classes soft, I've implemented a couple of interfaces to achieve above goal… So far so good.
The current issue is that the UI should also react to state changes on the hardware (a class that scan the status of the hardware once or twice a second).
Now to my question(s):
- Are there general rules how to structure (or where to put) the Listeners? (i.e. clump them all together in the JFrame - as one extreme or have one for each interactor (listening to "this").
- In my program the interfaces are implemented "unidirectional": the control classes are the "observer" the UI the "subject". How should I modify the program to make it bi-directional? The control classes are also subjects to be observed by the UI (since it has to update itself accordingly).
Many thanks for you input/suggestions/experience.