Link to home
Start Free TrialLog in
Avatar of Suppai
Suppai

asked on

What is best for class property change observing in a MVC design?

Im trying to design my app by a MVC design pattern. I want the gui and maybe some other parts (classes) of the app to react when the fields of one model class is changed. I have read that you can use a setup using java.beans.PropertyChangeSupport and java.beans.PropertyChangeListener, where you can define which events in the model class fields that fires the propertyChange event. However I have heard about a setup using the Observable interface. What is the most uptodate/best practice setup and which should I use?
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Use PropertyChangeSupport
Observable was added in Java 1.0 and is rarely (if ever) used these days

Avatar of Suppai
Suppai

ASKER

Ahh ok, that was what I needed to know. May have some questions regarding the java.beans setup later on, so Ill keep the question open for now.
Avatar of Suppai

ASKER


Hmm I cant make it work. I have made a specialized label for listening and changing according to changes in the model object. The code is attched "StatusLabelListener". Furthermore I have added the PropertyChangeSupport to my model object and added a method "addPropertyChangeListener to it for addidng the "StatusLabelListener". The listener is added as below:

StatusLabelListener statusBar = new StatusLabelListener();
link.addPropertyChangeListener(statusBar);

and the function for andding in the model object (instance named "link" here) is as simple as below:

public void addPropertyChangeListener(PropertyChangeListener pcl) {
     this.addPropertyChangeListener(pcl);
}

All I want is that when a status text property in the model changes then a status label ion the gui follows, and of course without having references of components in the gui in the model object. I tried following the examples, but nothing seems to work.
Avatar of Suppai

ASKER

Sorry forgot the "StatusLabelListener" code. Furthermore I have learned that the application simply terminates when reaching the line of code where the listener is addded to the lnk listener list:

link.addPropertyChangeListener(statusBar);
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
 
import javax.swing.JLabel;
import javax.swing.SwingConstants;
 
@SuppressWarnings("serial")
public class StatusLabelListener extends JLabel implements PropertyChangeListener {
 
	public StatusLabelListener() {
		super("",SwingConstants.RIGHT);
	}
	
	@Override
	public void propertyChange(PropertyChangeEvent evt) {
		this.setText((String)evt.getNewValue());
	}
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Suppai

ASKER

Youre right! One of my examples is wrong, the others refer to the Support object I can see. Thanks!