Example usage for javax.swing AbstractButton isSelected

List of usage examples for javax.swing AbstractButton isSelected

Introduction

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

Prototype

public boolean isSelected() 

Source Link

Document

Returns the state of the button.

Usage

From source file:Main.java

public static void main(String[] args) {
    AbstractButton jb = new JToggleButton("Press Me");
    jb.setSelected(true);//from ww w.ja  v a 2 s.c  o  m
    System.out.println(jb.isSelected());

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

From source file:FindingAButtonGroup.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Button Group");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel panel = new JPanel(new GridLayout(0, 1));
    Border border = BorderFactory.createTitledBorder("Examples");
    panel.setBorder(border);/*from  w  w w.j av a 2  s.  c  om*/
    ButtonGroup group = new ButtonGroup();
    AbstractButton abstract1 = new JToggleButton("Toggle Button");
    panel.add(abstract1);
    group.add(abstract1);
    AbstractButton abstract2 = new JRadioButton("Radio Button");
    panel.add(abstract2);
    group.add(abstract2);
    AbstractButton abstract3 = new JCheckBox("Check Box");
    panel.add(abstract3);
    group.add(abstract3);
    AbstractButton abstract4 = new JRadioButtonMenuItem("Radio Button Menu Item");
    panel.add(abstract4);
    group.add(abstract4);
    AbstractButton abstract5 = new JCheckBoxMenuItem("Check Box Menu Item");
    panel.add(abstract5);
    group.add(abstract5);
    frame.add(panel, BorderLayout.CENTER);
    frame.setSize(300, 200);
    frame.setVisible(true);
    group.setSelected(abstract1.getModel(), true);
    Enumeration elements = group.getElements();
    while (elements.hasMoreElements()) {
        AbstractButton button = (AbstractButton) elements.nextElement();
        if (button.isSelected()) {
            System.out.println("The winner is: " + button.getText());
        }
    }
}

From source file:SettingSelectedAButtonGroup.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Button Group");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel panel = new JPanel(new GridLayout(0, 1));
    Border border = BorderFactory.createTitledBorder("Examples");
    panel.setBorder(border);// w  ww  .j  a  v  a 2  s  . c  o m
    ButtonGroup group = new ButtonGroup();
    AbstractButton abstract1 = new JToggleButton("Toggle Button");
    panel.add(abstract1);
    group.add(abstract1);

    AbstractButton abstract2 = new JRadioButton("Radio Button");
    panel.add(abstract2);
    group.add(abstract2);

    AbstractButton abstract3 = new JCheckBox("Check Box");
    panel.add(abstract3);
    group.add(abstract3);

    AbstractButton abstract4 = new JRadioButtonMenuItem("Radio Button Menu Item");
    panel.add(abstract4);
    group.add(abstract4);

    AbstractButton abstract5 = new JCheckBoxMenuItem("Check Box Menu Item");
    panel.add(abstract5);
    group.add(abstract5);
    frame.add(panel, BorderLayout.CENTER);
    frame.setSize(300, 200);
    frame.setVisible(true);
    group.setSelected(abstract1.getModel(), true);

    Enumeration elements = group.getElements();
    while (elements.hasMoreElements()) {
        AbstractButton button = (AbstractButton) elements.nextElement();
        if (button.isSelected()) {
            System.out.println("The winner is: " + button.getText());
        }
    }
}

From source file:ASelectedButton.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Radio Buttons");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel panel = new JPanel(new GridLayout(0, 1));
    Border border = BorderFactory.createTitledBorder("Options");
    panel.setBorder(border);/*w  w  w. ja va 2s  . co m*/
    final ButtonGroup group = new ButtonGroup();
    AbstractButton abstract1 = new JRadioButton("One");
    panel.add(abstract1);
    group.add(abstract1);
    AbstractButton abstract2 = new JRadioButton("Two");
    panel.add(abstract2);
    group.add(abstract2);
    AbstractButton abstract3 = new JRadioButton("Three");
    panel.add(abstract3);
    group.add(abstract3);
    AbstractButton abstract4 = new JRadioButton("Four");
    panel.add(abstract4);
    group.add(abstract4);
    AbstractButton abstract5 = new JRadioButton("Five");
    panel.add(abstract5);
    group.add(abstract5);
    ActionListener aListener = new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            Enumeration elements = group.getElements();
            while (elements.hasMoreElements()) {
                AbstractButton button = (AbstractButton) elements.nextElement();
                if (button.isSelected()) {
                    System.out.println("The winner is: " + button.getText());
                    break;
                }
            }
            group.setSelected(null, true);
        }
    };
    JToggleButton button = new JToggleButton("Show Selected");
    button.addActionListener(aListener);
    Container container = frame.getContentPane();
    container.add(panel, BorderLayout.CENTER);
    container.add(button, BorderLayout.SOUTH);
    frame.setSize(300, 200);
    frame.setVisible(true);
}

From source file:Main.java

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

    JPanel panel = new JPanel(new GridLayout(0, 1));
    Border border = BorderFactory.createTitledBorder("Options");
    panel.setBorder(border);// w  w  w . j av a 2  s .c o m
    final ButtonGroup group = new ButtonGroup();
    AbstractButton abstract1 = new JRadioButton("One");
    panel.add(abstract1);
    group.add(abstract1);
    AbstractButton abstract2 = new JRadioButton("Two");
    panel.add(abstract2);
    group.add(abstract2);
    AbstractButton abstract3 = new JRadioButton("Three");
    panel.add(abstract3);
    group.add(abstract3);
    AbstractButton abstract4 = new JRadioButton("Four");
    panel.add(abstract4);
    group.add(abstract4);
    AbstractButton abstract5 = new JRadioButton("Five");
    panel.add(abstract5);
    group.add(abstract5);
    ActionListener aListener = new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            Enumeration elements = group.getElements();
            while (elements.hasMoreElements()) {
                AbstractButton button = (AbstractButton) elements.nextElement();
                if (button.isSelected()) {
                    System.out.println("The winner is: " + button.getText());
                    break;
                }
            }
            group.setSelected(null, true);
        }
    };
    JButton button = new JButton("Show Selected");
    button.addActionListener(aListener);
    Container container = frame.getContentPane();
    container.add(panel, BorderLayout.CENTER);
    container.add(button, BorderLayout.SOUTH);
    frame.setSize(300, 200);
    frame.setVisible(true);
}

From source file:Main.java

public static boolean isSelected(AbstractButton... buttons) {
    for (AbstractButton button : buttons) {
        if (button.isSelected()) {
            return true;
        }/*w w  w.  j a v a  2s. c  o  m*/
    }

    return false;
}

From source file:Main.java

public static AbstractButton getSelectedButton(ButtonGroup group) {
    Enumeration<AbstractButton> buttons = group.getElements();
    while (buttons.hasMoreElements()) {
        AbstractButton b = buttons.nextElement();
        if (b.isSelected()) {
            return b;
        }/*from www. ja  v a  2  s  .  c  om*/
    }
    return null;
}

From source file:Main.java

public static int getButtonGroupSelectIndex(ButtonGroup bg) {
    if (bg.getButtonCount() <= 0) {
        return -1;
    }//from  w  w  w  .jav  a  2s  . c  o m
    Enumeration<AbstractButton> bEnum = bg.getElements();
    for (int i = 0; bEnum.hasMoreElements(); i++) {
        AbstractButton temp = bEnum.nextElement();
        if (temp.isSelected()) {
            return i;
        }

    }
    return -1;
}

From source file:JRadioButtonSelectedElements.java

public static Enumeration<String> getSelectedElements(Container container) {
    Vector<String> selections = new Vector<String>();
    Component components[] = container.getComponents();
    for (int i = 0, n = components.length; i < n; i++) {
        if (components[i] instanceof AbstractButton) {
            AbstractButton button = (AbstractButton) components[i];
            if (button.isSelected()) {
                selections.addElement(button.getText());
            }//www  .j ava 2  s  . c om
        }
    }
    return selections.elements();
}

From source file:GroupRadio.java

public static Enumeration getSelectedElements(Container container) {
    Vector selections = new Vector();
    Component components[] = container.getComponents();
    for (int i = 0, n = components.length; i < n; i++) {
        if (components[i] instanceof AbstractButton) {
            AbstractButton button = (AbstractButton) components[i];
            if (button.isSelected()) {
                selections.addElement(button.getText());
            }/*from  w w w.  j  av  a 2 s. c  o  m*/
        }
    }
    return selections.elements();
}