Non-GUI objects and Java events

K

Krick

I'm trying to design something in Java and I've entered an area where
I've have no experience.

Essentially, I have a non-GUI object that has a lot of internal
variables, some of which are dependent on and also modify others.

So if I call MyObject.setVariable1(20), the object updates all other
dependent internal variables.

Now given that, I want to connect it to a GUI.

Each variable in my object will be connected to a GUI component that
will reflect the state of that variable.

In turn, each GUI component will modify its respective variable in the
object when the user changes the state of the component.

The piece of the puzzle that I'm missing is how to make an object that
can notify all the GUI controls that its state has changed.

I assume that it has to fire an event of some kind, and all the
controls have to listen for it. I'm at a loss as to how I do that.
Should I derive my object from component do I can piggyback off the
AWT event architecture?

Can anyone point me at some good source examples or articles that
might give me a push in the right direction?

....
Krick
 
C

Chris Smith

Krick said:
Each variable in my object will be connected to a GUI component that
will reflect the state of that variable.

In turn, each GUI component will modify its respective variable in the
object when the user changes the state of the component.

The piece of the puzzle that I'm missing is how to make an object that
can notify all the GUI controls that its state has changed.

Good question. This is, in fact a somewhat frequent and difficult
problem in retrofitting an MVC GUI to a data model that didn't keep
these things in mind. The steps I'd take are as follows:

1. Look really hard to see if there's any way you can add this
notification capability to the data model itself. That's by far the
cleanest way to handle your problem. The AWT event architecture is not
a prerequisite, but some kind of notification ability *is* a
prerequisite. (For example, Observer/Observable is acceptable, as is
some kind of domain-specific event architecture such as the DOM Level 2
Events spec for an XML DOM.)

2. If not, you'll need to wrap the data model into something that *can*
fire these events, *and* be sure that nothing in your system interacts
with the data model except through that wrapper, so that the appropriate
events always get fired.

3. If that isn't possible, you can build a caching bridge model between
the actual data structure and the component, and have this bridge poll
the actual data structure for changes on occasion. This "on occasion"
could be at regular timed intervals, or perhaps whenever you know that
the model may have changed.

Keep in mind that you don't need your data model to directly fire a
TreeModelEvent or ListModelEvent, nor should you want it to do so.
That's the job of the model adapter. (You are writing a model adapter,
right?) All you need do to fire some kind of event that the model
adapter can use, and then let the model adapter listen for that event
and respond by translating it to one of the standard GUI model events.
I assume that it has to fire an event of some kind, and all the
controls have to listen for it. I'm at a loss as to how I do that.
Should I derive my object from component do I can piggyback off the
AWT event architecture?

I assume you're talking about what I labelled approach #1 above. In
that case, you probably don't want to introduce dependencies between
your data model and the AWT (and you almost definitely don't want your
model classes to extend java.awt.Component). Instead, you just want
some kind of event model in place.

The AWT event model is fine for this use, because it's not connected to
the actual AWT classes; it's only an idiom that involves the use of a
certain relationship between objects and a few conventions for class
names. The javax.swing.event.EventListenerList class is definitely
helpful for implementing this idiom (and I'd make an exception here to
the general rule of not introducing dependencies between AWT/Swing and
your data model), but even that isn't really necessary. Just get some
kind of event notification working.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
O

Olaf Heimburger

I'm trying to design something in Java and I've entered an area where
I've have no experience.

<snip>

You should take a look at the JavaBeans (NOT EJB!) spec and check out
the definition of the PropertyChangeEvent/Listener construct. This
might help here.

--olaf
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,905
Latest member
Kristy_Poole

Latest Threads

Top