Is the Java Subclass property related or not?

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); } // Handle a property being updated static private void componentPropertyChange(java.beans.PropertyChangeEvent evt) { String propertyName = evt.getPropertyName(); System.out.println(MyBean.class.getSimpleName() + " - '" + propertyName + "' changed"); } } 

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.

+4
source share
1 answer

I have never seen it documented (or seen in the source) that all the given * methods call PropertyChangeEvent, let me know if you have and where. In general, if you want to catch method calls, you need to override it. If you do, be sure to call the supertype version from the overridden method using the "super" keyword.

You can call firePropertyChanged from the specified overridden method.

Outside of Swing code, you can provide a shell implementation, but this does not work with Swing, as it uses specific types instead of interfaces.

+1
source

Source: https://habr.com/ru/post/1439141/


All Articles