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?
SuppaiAsked:
Who is Participating?
 
objectsConnect With a Mentor Commented:
>      this.addPropertyChangeListener(pcl);

should be more like:

pcs..addPropertyChangeListener(pcl);

where pcs is your instance of PropertyChangeSupport

it handles all the listener management

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

0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
SuppaiAuthor Commented:
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.
0
 
SuppaiAuthor Commented:

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.
0
 
SuppaiAuthor Commented:
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

0
 
SuppaiAuthor Commented:
Youre right! One of my examples is wrong, the others refer to the Support object I can see. Thanks!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.