We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

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

Suppai
Suppai asked
on
Medium Priority
341 Views
Last Modified: 2013-11-23
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?
Comment
Watch Question

Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

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

Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:

Author

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.

Author

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.

Author

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

Java Developer
CERTIFIED EXPERT
Top Expert 2010
Commented:
>      this.addPropertyChangeListener(pcl);

should be more like:

pcs..addPropertyChangeListener(pcl);

where pcs is your instance of PropertyChangeSupport

it handles all the listener management

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
Youre right! One of my examples is wrong, the others refer to the Support object I can see. Thanks!
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.