I have a question regarding the use of related properties when using Swing components. So, I have this very simple Java class extending the JButton class:
public class MyBean extends JButton { public static final String PROP_SAMPLE_PROPERTY = "sampleProperty"; private String sampleProperty; public MyBean() { addPropertyChangeListener(new java.beans.PropertyChangeListener() { @Override public void propertyChange(java.beans.PropertyChangeEvent evt) { componentPropertyChange(evt); } }); } public String getSampleProperty() { return sampleProperty; } public void setSampleProperty(String value) { String oldValue = sampleProperty; sampleProperty = value; firePropertyChange(PROP_SAMPLE_PROPERTY, oldValue, sampleProperty); }
Here is my main class:
public static void main(String[] args) { try { Class<?> clazz = MyBean.class; BeanInfo bi = Introspector.getBeanInfo(clazz); PropertyDescriptor[] pds = bi.getPropertyDescriptors(); for (PropertyDescriptor pd : pds) { System.out.println(String.format("%s - %s - %s", clazz.getSimpleName(), pd.getName(), Boolean.toString(pd.isBound()))); } MyBean myBean = new MyBean(); myBean.setText("My name"); myBean.setLocation(new Point(10,10)); myBean.setVisible(true); } catch (IntrospectionException e) { e.printStackTrace(); } }
The launch of this application will be displayed:
MyBean - UI - true MyBean - UIClassID - true MyBean - accessibleContext - true MyBean - action - true ... MyBean - vetoableChangeListeners - true MyBean - visible - true MyBean - visibleRect - true MyBean - width - true MyBean - x - true MyBean - y - true **MyBean - 'text' changed**
My question is:
For all MyBean properties, a line is printed indicating that the property is bound, however, when I create an instance of MyBean and call setText, setLocation and setVisible only in case of setText, a line is printed. My conclusion (possibly wrong) is that if setLocation and setVisible are called, the firePropertyChange method is not called, so PropertyChangedListener is not called. Now I thought that the firePropertyChange method is called for each binding property when it is updated? This is apparently not the case, or the printed list may be incorrect. Any explanation for this would be greatly appreciated.
source share