PropertyChangeListener not recognizing changes to a putClientProperty  
Author Message
hotshot2000





PostPosted: 2004-4-26 0:08:00 Top

java-programmer, PropertyChangeListener not recognizing changes to a putClientProperty 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);
}
}
 
Michael Rauscher





PostPosted: 2004-4-26 1:26:00 Top

java-programmer >> PropertyChangeListener not recognizing changes to a putClientProperty Hi Will,

Will schrieb:
> 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

 
hotshot2000





PostPosted: 2004-4-26 16:54:00 Top

java-programmer >> PropertyChangeListener not recognizing changes to a putClientProperty Michael Rauscher <email***@***.com> wrote in message news:<c6gruq$8t7$03$email***@***.com>...
> Hi Will,
>
> Will schrieb:
> > 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

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
 
 
Michael Rauscher





PostPosted: 2004-4-30 1:34:00 Top

java-programmer >> PropertyChangeListener not recognizing changes to a putClientProperty Hi Will,

Will schrieb:
> Michael Rauscher <email***@***.com> wrote in message news:<c6gruq$8t7$03$email***@***.com>...
>
>>Hi Will,
>>
>>Will schrieb:
>>
>>>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
>
>
> 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