Example usage for javax.swing ButtonModel isSelected

List of usage examples for javax.swing ButtonModel isSelected

Introduction

In this page you can find the example usage for javax.swing ButtonModel isSelected.

Prototype

boolean isSelected();

Source Link

Document

Indicates if the button has been selected.

Usage

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  2s .c o m*/
    };

    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);
}

From source file:JCheckBoxChangeListener.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Iconizing CheckBox");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JCheckBox aCheckBox4 = new JCheckBox("Stuffed Crust");

    // Define ChangeListener
    ChangeListener changeListener = new ChangeListener() {
        public void stateChanged(ChangeEvent changeEvent) {
            AbstractButton abstractButton = (AbstractButton) changeEvent.getSource();
            ButtonModel buttonModel = abstractButton.getModel();
            boolean armed = buttonModel.isArmed();
            boolean pressed = buttonModel.isPressed();
            boolean selected = buttonModel.isSelected();
            System.out.println("Changed: " + armed + "/" + pressed + "/" + selected);
        }//from w  ww  .  ja v  a 2  s.co m
    };

    aCheckBox4.addChangeListener(changeListener);
    frame.add(aCheckBox4);
    frame.setSize(300, 200);
    frame.setVisible(true);
}

From source file:Main.java

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

    ChangeListener changeListener = new ChangeListener() {
        public void stateChanged(ChangeEvent changeEvent) {
            AbstractButton abstractButton = (AbstractButton) changeEvent.getSource();
            ButtonModel buttonModel = abstractButton.getModel();
            boolean armed = buttonModel.isArmed();
            boolean pressed = buttonModel.isPressed();
            boolean selected = buttonModel.isSelected();
            System.out.println("Changed: " + armed + "/" + pressed + "/" + selected);
        }/*from   w  ww. jav  a 2  s  .c o m*/
    };

    // Attach Listeners
    toggleButton.addChangeListener(changeListener);
    frame.add(toggleButton, BorderLayout.NORTH);
    frame.setSize(300, 125);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Grouping Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel panel = new JPanel(new GridLayout(0, 1));

    ButtonGroup group = new ButtonGroup();
    JRadioButton aRadioButton = new JRadioButton("A");
    JRadioButton bRadioButton = new JRadioButton("B");

    ChangeListener changeListener = new ChangeListener() {
        public void stateChanged(ChangeEvent changEvent) {
            AbstractButton aButton = (AbstractButton) changEvent.getSource();
            ButtonModel aModel = aButton.getModel();
            boolean armed = aModel.isArmed();
            boolean pressed = aModel.isPressed();
            boolean selected = aModel.isSelected();
            System.out.println("Changed: " + armed + "/" + pressed + "/" + selected);
        }/* w w w.  ja v a  2s.  c  om*/
    };

    panel.add(aRadioButton);
    group.add(aRadioButton);
    panel.add(bRadioButton);
    group.add(bRadioButton);

    aRadioButton.addChangeListener(changeListener);
    bRadioButton.addChangeListener(changeListener);

    frame.add(panel);
    frame.setSize(300, 200);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Selecting CheckBox");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JCheckBox checkBox = new JCheckBox("A");

    // Define ChangeListener
    ChangeListener changeListener = new ChangeListener() {
        public void stateChanged(ChangeEvent changeEvent) {
            AbstractButton abstractButton = (AbstractButton) changeEvent.getSource();
            ButtonModel buttonModel = abstractButton.getModel();
            boolean armed = buttonModel.isArmed();
            boolean pressed = buttonModel.isPressed();
            boolean selected = buttonModel.isSelected();
            System.out.println("Changed: " + armed + "/" + pressed + "/" + selected);
        }/*from   w w  w. ja v  a 2s  . c o m*/
    };
    // Attach Listeners
    checkBox.addChangeListener(changeListener);
    checkBox.setMnemonic(KeyEvent.VK_S);
    frame.add(checkBox, BorderLayout.NORTH);
    frame.setSize(300, 100);
    frame.setVisible(true);
}

From source file:JToggleButtonEventListenerSequence.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Selecting Toggle");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JToggleButton toggleButton = new JToggleButton("Toggle Button");
    // Define ActionListener
    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            AbstractButton abstractButton = (AbstractButton) actionEvent.getSource();
            boolean selected = abstractButton.getModel().isSelected();
            System.out.println("Action - selected=" + selected + "\n");
        }// ww  w  .j  a  v a2s.  c om
    };
    // Define ChangeListener
    ChangeListener changeListener = new ChangeListener() {
        public void stateChanged(ChangeEvent changeEvent) {
            AbstractButton abstractButton = (AbstractButton) changeEvent.getSource();
            ButtonModel buttonModel = abstractButton.getModel();
            boolean armed = buttonModel.isArmed();
            boolean pressed = buttonModel.isPressed();
            boolean selected = buttonModel.isSelected();
            System.out.println("Changed: " + armed + "/" + pressed + "/" + selected);
        }
    };
    // Define ItemListener
    ItemListener itemListener = new ItemListener() {
        public void itemStateChanged(ItemEvent itemEvent) {
            int state = itemEvent.getStateChange();
            if (state == ItemEvent.SELECTED) {
                System.out.println("Selected");
            } else {
                System.out.println("Deselected");
            }
        }
    };
    // Attach Listeners
    toggleButton.addActionListener(actionListener);
    toggleButton.addChangeListener(changeListener);
    toggleButton.addItemListener(itemListener);
    frame.add(toggleButton, BorderLayout.NORTH);
    frame.setSize(300, 125);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String args[]) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JToggleButton toggleButton = new JToggleButton("Selected");
    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            AbstractButton abstractButton = (AbstractButton) actionEvent.getSource();
            boolean selected = abstractButton.getModel().isSelected();
            System.out.println("Action - selected=" + selected + "\n");
        }//from   w  w w .ja  va2 s.c  o  m
    };
    ChangeListener changeListener = new ChangeListener() {
        public void stateChanged(ChangeEvent changeEvent) {
            AbstractButton abstractButton = (AbstractButton) changeEvent.getSource();
            ButtonModel buttonModel = abstractButton.getModel();
            boolean armed = buttonModel.isArmed();
            boolean pressed = buttonModel.isPressed();
            boolean selected = buttonModel.isSelected();
            System.out.println("Changed: " + armed + "/" + pressed + "/" + selected);
        }
    };
    ItemListener itemListener = new ItemListener() {
        public void itemStateChanged(ItemEvent itemEvent) {
            int state = itemEvent.getStateChange();
            if (state == ItemEvent.SELECTED) {
                System.out.println("Selected");
            } else {
                System.out.println("Deselected");
            }
        }
    };
    // Attach Listeners
    toggleButton.addActionListener(actionListener);
    toggleButton.addChangeListener(changeListener);
    toggleButton.addItemListener(itemListener);
    Container contentPane = frame.getContentPane();
    contentPane.add(toggleButton, BorderLayout.NORTH);

    JToggleButton toggleButton2 = new JToggleButton("Focused");
    contentPane.add(toggleButton2, BorderLayout.CENTER);

    JToggleButton toggleButton3 = new JToggleButton("Not Selected");
    contentPane.add(toggleButton3, BorderLayout.SOUTH);

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

From source file:SelectingCheckBox.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Selecting CheckBox");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JCheckBox checkBox = new JCheckBox(DESELECTED_LABEL);
    // Define ActionListener
    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            AbstractButton abstractButton = (AbstractButton) actionEvent.getSource();
            boolean selected = abstractButton.getModel().isSelected();
            String newLabel = (selected ? SELECTED_LABEL : DESELECTED_LABEL);
            abstractButton.setText(newLabel);
        }/*  w  w  w  . j  a v  a 2  s .com*/
    };
    ChangeListener changeListener = new ChangeListener() {
        public void stateChanged(ChangeEvent changeEvent) {
            AbstractButton abstractButton = (AbstractButton) changeEvent.getSource();
            ButtonModel buttonModel = abstractButton.getModel();
            boolean armed = buttonModel.isArmed();
            boolean pressed = buttonModel.isPressed();
            boolean selected = buttonModel.isSelected();
            System.out.println("Changed: " + armed + "/" + pressed + "/" + selected);
        }
    };

    ItemListener itemListener = new ItemListener() {
        public void itemStateChanged(ItemEvent itemEvent) {
            AbstractButton abstractButton = (AbstractButton) itemEvent.getSource();
            Color foreground = abstractButton.getForeground();
            Color background = abstractButton.getBackground();
            int state = itemEvent.getStateChange();
            if (state == ItemEvent.SELECTED) {
                abstractButton.setForeground(background);
                abstractButton.setBackground(foreground);
            }
        }
    };

    checkBox.addActionListener(actionListener);
    checkBox.addChangeListener(changeListener);
    checkBox.addItemListener(itemListener);
    checkBox.setMnemonic(KeyEvent.VK_S);
    Container contentPane = frame.getContentPane();
    contentPane.add(checkBox, BorderLayout.NORTH);
    frame.setSize(300, 100);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] a) {
    final String DESELECTED_LABEL = "Deselected";
    final String SELECTED_LABEL = "Selected";

    JFrame frame = new JFrame("Selecting CheckBox");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JCheckBox checkBox = new JCheckBox(DESELECTED_LABEL);

    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            AbstractButton abstractButton = (AbstractButton) actionEvent.getSource();
            boolean selected = abstractButton.getModel().isSelected();
            String newLabel = (selected ? SELECTED_LABEL : DESELECTED_LABEL);
            abstractButton.setText(newLabel);
        }//from   w ww  . j  a  va  2s . co  m
    };

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

    ItemListener itemListener = new ItemListener() {
        public void itemStateChanged(ItemEvent itemEvent) {
            AbstractButton abstractButton = (AbstractButton) itemEvent.getSource();
            Color foreground = abstractButton.getForeground();
            Color background = abstractButton.getBackground();
            int state = itemEvent.getStateChange();
            if (state == ItemEvent.SELECTED) {
                abstractButton.setForeground(background);
                abstractButton.setBackground(foreground);
            }
        }
    };

    checkBox.getModel().addActionListener(actionListener);
    checkBox.getModel().addChangeListener(changeListener);
    checkBox.getModel().addItemListener(itemListener);

    frame.add(checkBox, BorderLayout.NORTH);
    frame.setSize(300, 100);
    frame.setVisible(true);
}

From source file:MainClass.java

public static void main(String[] a) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JToggleButton toggleButton = new JToggleButton("Selected");

    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            AbstractButton abstractButton = (AbstractButton) actionEvent.getSource();
            boolean selected = abstractButton.getModel().isSelected();
            System.out.println("Action - selected=" + selected + "\n");
        }//w  ww.  j av a  2 s. co  m
    };

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

    ItemListener itemListener = new ItemListener() {
        public void itemStateChanged(ItemEvent itemEvent) {
            int state = itemEvent.getStateChange();
            if (state == ItemEvent.SELECTED) {
                System.out.println("Selected");
            } else {
                System.out.println("Deselected");
            }
        }
    };

    toggleButton.addActionListener(actionListener);
    toggleButton.addChangeListener(changeListener);
    toggleButton.addItemListener(itemListener);
    frame.add(toggleButton, BorderLayout.NORTH);

    JToggleButton toggleButton2 = new JToggleButton("Focused");
    frame.add(toggleButton2, BorderLayout.CENTER);

    JToggleButton toggleButton3 = new JToggleButton("Not Selected");
    frame.add(toggleButton3, BorderLayout.SOUTH);

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