JComponent: addPropertyChangeListener(PropertyChangeListener listener) : JComponent « javax.swing « Java by API






JComponent: addPropertyChangeListener(PropertyChangeListener listener)

  

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 ActionChangedListener implements PropertyChangeListener {
  private JButton button;
  public ActionChangedListener(JButton button) {
    this.button = button;
  }
  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 ActionChangedListener(bn));
    frame.add(bn);
    bn.setEnabled(false);
    frame.setSize(300, 300);
    frame.setVisible(true);
    frame.add(bn);
  }
}

   
    
  








Related examples in the same category

1.JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT
2.JComponent.WHEN_IN_FOCUSED_WINDOW
3.JComponent: addVetoableChangeListener(VetoableChangeListener listener)
4.JComponent: getAccessibleContext()
5.JComponent: getActionMap()
6.JComponent: getHeight()
7.JComponent: getInputMap()
8.JComponent: getTransferHandler()
9.JComponent: getWidth()
10.JComponent: getX()
11.JComponent: getY()
12.JComponent: putClientProperty(Object key, Object value)
13.JComponent: requestFocusInWindow()
14.JComponent: registerKeyboardAction(ActionListener anAction, KeyStroke aKeyStroke, int aCondition)
15.JComponent: revalidate()
16.JComponent: setComponentPopupMenu(JPopupMenu popup)
17.JComponent: setFont(Font font)
18.JComponent: setFocusTraversalKeys(int arg0, Set arg1)
19.JComponent: setInputMap(int condition, InputMap map)
20.JComponent: setInputVerifier(InputVerifier inputVerifier)
21.JComponent: setOpaque(boolean isOpaque)
22.JComponent: setTransferHandler(TransferHandler newHandler)