Example usage for javax.swing JButton addPropertyChangeListener

List of usage examples for javax.swing JButton addPropertyChangeListener

Introduction

In this page you can find the example usage for javax.swing JButton addPropertyChangeListener.

Prototype

public void addPropertyChangeListener(PropertyChangeListener listener) 

Source Link

Document

Adds a PropertyChangeListener to the listener list.

Usage

From source file:Main.java

License:asdf

public static void main(String[] args) {
    JButton bn = new JButton("asdf");
    bn.addPropertyChangeListener(new AbstractButtonPropertyChangeListener());

    bn.setText("java2s.com");

    JOptionPane.showMessageDialog(null, bn);
}

From source file:ActionChangedListener.java

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);/*from  w  ww. jav  a2  s .c om*/
    bn.setEnabled(false);
    frame.setSize(300, 300);
    frame.setVisible(true);
    frame.add(bn);
}

From source file:ActionChangedListener.java

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);/*from w  w w.  j  av a 2  s.com*/
    bn.setEnabled(false);

    frame.setSize(300, 300);
    frame.setVisible(true);

    frame.add(bn);
}

From source file:Main.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Button Sample");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    final JButton button1 = new JButton("Select Me");
    final JButton button2 = new JButton("No Select Me");
    final Random random = new Random();

    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            JButton button = (JButton) actionEvent.getSource();
            int red = random.nextInt(255);
            int green = random.nextInt(255);
            int blue = random.nextInt(255);
            button.setBackground(new Color(red, green, blue));
        }//from   w ww .  jav  a2 s. co  m
    };

    PropertyChangeListener propertyChangeListener = new PropertyChangeListener() {
        public void propertyChange(PropertyChangeEvent propertyChangeEvent) {
            String property = propertyChangeEvent.getPropertyName();
            if ("background".equals(property)) {
                button2.setBackground((Color) propertyChangeEvent.getNewValue());
            }
        }
    };

    button1.addActionListener(actionListener);
    button1.addPropertyChangeListener(propertyChangeListener);
    button2.addActionListener(actionListener);

    frame.add(button1, BorderLayout.NORTH);
    frame.add(button2, BorderLayout.SOUTH);
    frame.setSize(300, 100);
    frame.setVisible(true);
}

From source file:PropertyChangeListenerDemo.java

public static void main(String args[]) {
    Runnable runner = new Runnable() {
        public void run() {
            JFrame frame = new JFrame("Button Sample");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            final JButton button1 = new JButton("Select Me");
            final JButton button2 = new JButton("No Select Me");
            final Random random = new Random();
            // Define ActionListener
            ActionListener actionListener = new ActionListener() {
                public void actionPerformed(ActionEvent actionEvent) {
                    JButton button = (JButton) actionEvent.getSource();
                    int red = random.nextInt(255);
                    int green = random.nextInt(255);
                    int blue = random.nextInt(255);
                    button.setBackground(new Color(red, green, blue));
                }/* w w w .  jav  a2  s  .  c o m*/
            };
            // Define PropertyChangeListener
            PropertyChangeListener propertyChangeListener = new PropertyChangeListener() {
                public void propertyChange(PropertyChangeEvent propertyChangeEvent) {
                    String property = propertyChangeEvent.getPropertyName();
                    if ("background".equals(property)) {
                        button2.setBackground((Color) propertyChangeEvent.getNewValue());
                    }
                }
            };
            button1.addActionListener(actionListener);
            button1.addPropertyChangeListener(propertyChangeListener);
            button2.addActionListener(actionListener);
            frame.add(button1, BorderLayout.NORTH);
            frame.add(button2, BorderLayout.SOUTH);
            frame.setSize(300, 100);
            frame.setVisible(true);
        }
    };
    EventQueue.invokeLater(runner);
}