Example usage for javax.swing JButton addChangeListener

List of usage examples for javax.swing JButton addChangeListener

Introduction

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

Prototype

public void addChangeListener(ChangeListener l) 

Source Link

Document

Adds a ChangeListener to the button.

Usage

From source file:Main.java

public static void main(String[] args) {
    JButton button = new JButton("ok");
    button.addChangeListener(new ChangeListener() {
        public void stateChanged(ChangeEvent e) {
            DefaultButtonModel model = (DefaultButtonModel) button.getModel();
            if (model.isEnabled())
                System.out.println("Enabled: true");
            else/*from  ww w. j  av  a2  s  .c o m*/
                System.out.println("Enabled: false");

            if (model.isArmed())
                System.out.println("Armed: true");
            else
                System.out.println("Armed: false");

            if (model.isPressed())
                System.out.println("Pressed: true");
            else
                System.out.println("Pressed: false");
        }
    });
    JOptionPane.showMessageDialog(null, button);
}

From source file:ButtonModel.java

public static void main(String[] args) {
    final JButton ok = new JButton("ok");

    JCheckBox cb = new JCheckBox("Enabled", true);

    ok.setBounds(40, 30, 80, 25);//from  w  w  w . j  ava 2  s .  co  m
    ok.addChangeListener(new ChangeListener() {
        public void stateChanged(ChangeEvent e) {
            DefaultButtonModel model = (DefaultButtonModel) ok.getModel();
            if (model.isEnabled())
                System.out.println("Enabled: true");
            else
                System.out.println("Enabled: false");

            if (model.isArmed())
                System.out.println("Armed: true");
            else
                System.out.println("Armed: false");

            if (model.isPressed())
                System.out.println("Pressed: true");
            else
                System.out.println("Pressed: false");
        }

    });

    cb.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            if (ok.isEnabled())
                ok.setEnabled(false);
            else
                ok.setEnabled(true);
        }
    });

    JFrame f = new JFrame();
    f.setLayout(new FlowLayout());
    f.add(ok);
    f.add(cb);
    f.setSize(350, 250);
    f.setLocationRelativeTo(null);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);
}

From source file:MainClass.java

public static void main(String[] args) {
    JButton jb = new JButton("Press Me");

    jb.addItemListener(new ItemListener() {
        public void itemStateChanged(ItemEvent ev) {
            System.out.println("ItemEvent!");
        }/*from  ww  w  . j  a  v a2  s. co  m*/
    });

    jb.addChangeListener(new ChangeListener() {
        public void stateChanged(ChangeEvent ev) {
            System.out.println("ChangeEvent!");
        }
    });

    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.getContentPane().add(jb);
    f.pack();
    f.setVisible(true);
}

From source file:SelectingButton.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Selecting Button");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JButton theButton = new JButton("Button");

    ActionListener aListener = new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            AbstractButton aButton = (AbstractButton) event.getSource();
            boolean selected = aButton.getModel().isSelected();
            System.out.println("Action - selected=" + selected + "\n");
        }//w  w  w  .  j ava 2  s.com
    };

    ChangeListener cListener = new ChangeListener() {
        public void stateChanged(ChangeEvent event) {
            AbstractButton aButton = (AbstractButton) event.getSource();
            ButtonModel aModel = aButton.getModel();
            boolean armed = aModel.isArmed();
            boolean pressed = aModel.isPressed();
            boolean selected = aModel.isSelected();
            System.out.println("Changed: " + armed + "/" + pressed + "/" + selected);
        }
    };

    theButton.addActionListener(aListener);
    theButton.addChangeListener(cListener);
    Container contentPane = frame.getContentPane();
    contentPane.add(theButton, BorderLayout.CENTER);
    frame.setSize(300, 200);
    frame.setVisible(true);
}