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.PropertyChangeS upport and java.beans.PropertyChangeL istener, 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?
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.
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
StatusLabelListener statusBar = new StatusLabelListener();
link.addPropertyChangeList
and the function for andding in the model object (instance named "link" here) is as simple as below:
public void addPropertyChangeListener(
this.addPropertyChangeList
}
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.
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.addPropertyChangeList ener(statu sBar);
link.addPropertyChangeList
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());
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Youre right! One of my examples is wrong, the others refer to the Support object I can see. Thanks!
Observable was added in Java 1.0 and is rarely (if ever) used these days