• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 322
  • Last Modified:

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?
0
Suppai
Asked:
Suppai
  • 4
  • 3
1 Solution
 
objectsCommented:
Use PropertyChangeSupport
Observable was added in Java 1.0 and is rarely (if ever) used these days

0
 
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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
 
objectsCommented:
>      this.addPropertyChangeListener(pcl);

should be more like:

pcs..addPropertyChangeListener(pcl);

where pcs is your instance of PropertyChangeSupport

it handles all the listener management

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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now