PropertyChangeListener not recognizing changes to a putClientProperty

W

Will

I must be missing out on something fundamental . . . why doesn't the
following code ever register a PropertyChangeEvent?

import java.beans.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.text.*;

public class PropTest extends JFrame {
private Boolean myBoolean = Boolean.FALSE;

public PropTest() {
super("PropertyChangeListener Test");
JPanel panel = new JPanel();
JButton button = new JButton(new BooleanAction("Button pushed"));
button.setText("Push me!");
panel.putClientProperty("myBoolean", myBoolean);
panel.addPropertyChangeListener("myBoolean", new BooleanListener());
panel.add(button);
panel.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0),
"space");
panel.getActionMap().put("space", new BooleanAction("Space
pressed"));
this.getContentPane().add(panel);
}

private class BooleanAction extends AbstractAction {
String title;
public BooleanAction(String title) {
super();
this.title = title;
}

public void actionPerformed(ActionEvent e) {
System.out.println(title);
System.out.println("Old value: " + myBoolean.toString());
myBoolean = new Boolean(!myBoolean.booleanValue());
System.out.println("New value: " + myBoolean.toString());
}
}

private class BooleanListener implements PropertyChangeListener {
public void propertyChange(PropertyChangeEvent e) {
System.out.println("Hey, you changed my value!");
}
}

public static void main(String[] args) {
PropTest propTest = new PropTest();
propTest.setResizable(false);
propTest.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
propTest.pack();
propTest.setVisible(true);
}
}
 
M

Michael Rauscher

Hi Will,
I must be missing out on something fundamental . . . why doesn't the
following code ever register a PropertyChangeEvent?

Perhaps it's because you don't change the property?

E.g. declare panel as instance variable, remove the local declaration
from your PropTest constructor and add
panel.putClientProperty("myBoolean", myBoolean) to your actionPerformed
method.

Bye
Michael
 
W

Will

Michael Rauscher said:
Hi Will,


Perhaps it's because you don't change the property?

E.g. declare panel as instance variable, remove the local declaration
from your PropTest constructor and add
panel.putClientProperty("myBoolean", myBoolean) to your actionPerformed
method.

Bye
Michael

Thanks for the help, that did it. I still don't understand the logic,
though -- could you help clarify my understanding? I thought that by
registering something in the client property hashtable, any change to
the value key would trigger a change event; why must one actually
reregister the key/value pair? It seems analagous to working with the
standard keys, like "font" and "background", which register a property
change when one merely calls component.setFont() or
component.setBackground() (obviously, when such a call results in a
change)? Do these methods actually reregister the pair in the
hashtable?

Thanks again,

Will
 
M

Michael Rauscher

Hi Will,
Thanks for the help, that did it. I still don't understand the logic,
though -- could you help clarify my understanding? I thought that by

Perhaps ;-)
registering something in the client property hashtable, any change to
the value key would trigger a change event; why must one actually
reregister the key/value pair? It seems analagous to working with the

A client property stores a name and a reference to an object. The event
is fired whenever the reference is set.

The value of a client property is a reference to an object. If you don't
change this reference, you don't change the value of the property.
standard keys, like "font" and "background", which register a property
change when one merely calls component.setFont() or
component.setBackground() (obviously, when such a call results in a
change)? Do these methods actually reregister the pair in the
hashtable?

The font property e.g. is a so called "bound property". A bound property
is a property that fires PropertyChangeEvents whenever its value is
changed. Class java.beans.PropertyChangeSupport may be used to support
bound properties in your bean.

e.g.

class PropertyTest implements Serializable {
private String info = "";
private PropertyChangeSupport listeners = new
PropertyChangeSupport(this);

public String getInfo() {
return info;
}

public void setInfo( String newInfo ) {
String oldInfo = info;
info = newInfo;
listeners.firePropertyChange( "info", oldInfo, newInfo );
}

// code to add and remove PropertyChangeListener objects
// to instance variable listeners
}

If you call setInfo on a PropertyTest object, the registered
PropertyChangeListener objects are notified about this change.

Bye
Michael
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top