Java Beans PropertyChangeListener handle property change event
import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; import javax.swing.Action; import javax.swing.Icon; import javax.swing.JButton; import javax.swing.JFrame; class My implements PropertyChangeListener { private JButton button; public My(JButton button) { this.button = button; }/* w w w . java 2 s . com*/ public void propertyChange(PropertyChangeEvent e) { String propertyName = e.getPropertyName(); System.out.println(propertyName); if (e.getPropertyName().equals(Action.NAME)) { String text = (String) e.getNewValue(); button.setText(text); button.repaint(); } else if (propertyName.equals("enabled")) { Boolean enabledState = (Boolean) e.getNewValue(); button.setEnabled(enabledState.booleanValue()); button.repaint(); } else if (e.getPropertyName().equals(Action.SMALL_ICON)) { Icon icon = (Icon) e.getNewValue(); button.setIcon(icon); button.invalidate(); button.repaint(); } } } public class Main { public static void main(String[] a) { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JButton bn = new JButton(); bn.addPropertyChangeListener(new My(bn)); frame.add(bn); bn.setEnabled(false); frame.setSize(300, 300); frame.setVisible(true); frame.add(bn); } }