Search code examples
javaswingpropertychangelistener

PropertyChange Listener for icons


I am trying to write a PropertyChange Listener which detects whether a JButton has an ImageIcon or not.

"icon" is a beans property but I can't seem to add iconListener or something equivalent to the PropertyChangeListener.

Is there really no specific listener for icons ?

From the Oracle tutorial : "Property-change events occur whenever the value of a bound property changes for a bean ??? a component that conforms to the JavaBeans™ specification."

buttonArray[a].addChangeListener("icon",new iconListener()); // doesn't work

buttonArray[a].addChangeListener("icon", new ImageIconListener()); // doesn't work

Solution

  • Nothing special to the icon property, it's just a ... property :-) So you need a custom PropertyChangeListener and register it with the button

    PropertyChangeListener myListener = new PropertyChangeListener() .... {
    
    };
    // register to be only notified if the _icon_ property changes
    button.addPropertyChangeListener("icon", myListener);