views:

572

answers:

4

Hi All

I have a Java GUI that has a number of text fields, the values of which are populated from static variable in another class.

I am interested to know what the best way is to make it so that when the variable is changed in another class, the update is instantly reflected on the GUI.

If any one could make a suggestion on an efficient way to do this it would be highly appreciated.

Many Thanks for your replies in advance

Edit:

Additional Details

  • Using Swing
  • Updates would ideally be real time
A: 

You didn't mention what toolkit you were using and what the architecture of your project is.

It's also not clear how fast and frequently you want to update the GUI elements, as the cost in flickrers may be too great, and so might be the cost in notifications using a listening mechanism.

If it is fairly straightforward to determine change, and if I don't need real-time performance but rather just some feedback to the users, I've been happy enough running a background thread that occasionally makes the check and updates if necessary.

Uri
Updated with additional details. Apologies for being unclear.
Mark Davidson
A: 

The easiest way is to first control all access to that static variable. Make it private, and modifiable only via some single method. Secondly, have that method call setText() on all those text fields, and set them to the new value.

If that variable's value can be changed outside the Event Dispatch Thread, then that method must use SwingUtilities.invokeLater() to set those text fields; components should only be accessed by the EDT.

If each text field's display is computed by multiple decentralized methods, then your variable-setting method just needs to call those methods (possibly wrapped in invokeLater() as above).

If the variable-modification code doesn't know ahead of time which text fields will be affected, then you need to allow the objects controlling each text field to add themselves to the modification code as listeners. (This is MVC design, and generally considered to be Good.) Then the modification code calls each listener in its list and lets it know the value has changed.

Paul Brinkley
+3  A: 

In my opinion a wise choice is to implement it following the Observer Pattern. you can find plenty of examples on this topic like this

Stefano Driussi
A: 

Observer/Observable using listeners. Here with an instance variable:

public class StaticVarClass {
    public static interface Listener {
        public void varChanged(Object oldVar, Object newVar);
    }

    private Object var;

    private List<Listener> listeners = new CopyOnWriteArrayList<Listener>();

    public void addListener(Listener l) { listeners.add(l); }
    public void removeListener(Listener l) { listeners.remove(l); }

    protected void fireVarChanged(Object oldVar, Object newVar) {
        for (listener : listeners) listener.varChanged(oldVar, newVar);
    }

    public void setVar(Object newVar) {
        fireVarChanged(var, var = newVar);
    }

Of course you could use the inbuilt PropertyChangeListener mechanism also.

oxbow_lakes