Example usage for javax.swing ButtonGroup getElements

List of usage examples for javax.swing ButtonGroup getElements

Introduction

In this page you can find the example usage for javax.swing ButtonGroup getElements.

Prototype

public Enumeration<AbstractButton> getElements() 

Source Link

Document

Returns all the buttons that are participating in this group.

Usage

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);/*from w w w .  jav  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);
        }
    };
    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);/*ww w . ja v a 2  s .c om*/
    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: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);/*  ww w . ja  v  a 2  s  . co 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: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);/*from   www . j av  a 2  s  . co 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:Main.java

/**
 * Removes all buttons from a button group.
 * @param group the button group/* w  w  w  .j  a  va  2 s  .c  o  m*/
 */
public static void clearButtonGroup(ButtonGroup group) {
    while (group.getElements().hasMoreElements()) {
        group.remove(group.getElements().nextElement());
    }
}

From source file:Main.java

public static JRadioButton getSelection(ButtonGroup group) {
    for (Enumeration e = group.getElements(); e.hasMoreElements();) {
        JRadioButton b = (JRadioButton) e.nextElement();
        if (b.getModel() == group.getSelection()) {
            return b;
        }/*from  w ww  .  ja v  a  2  s.c o m*/
    }
    return null;
}

From source file:Main.java

/**
 * This method returns the selected radio button in a button group.
 * @param group the button group//from   w w w  .  j a  v a 2 s  .  c om
 * @return the selected radio button
 */
public static JRadioButton getSelectedRadioButton(ButtonGroup group) {
    for (Enumeration e = group.getElements(); e.hasMoreElements();) {
        JRadioButton b = (JRadioButton) e.nextElement();
        if (b.getModel() == group.getSelection()) {
            return b;
        }
    }
    return null;
}

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 ww  w . j  a  v a2  s. c  o m
    }
    return null;
}

From source file:Main.java

public static void setButtonGroupEnabled(ButtonGroup buttonGroup, boolean enabled) {
    Enumeration<AbstractButton> buttonEnum = buttonGroup.getElements();
    while (buttonEnum.hasMoreElements()) {
        buttonEnum.nextElement().setEnabled(enabled);
    }/*from   www  .j a  v  a 2s .  c o  m*/
}

From source file:Main.java

/**
 * Sets the background color for the specified <code>ButtonGroup</code> and
 * all the JCheckBox, JComboBox, JButton, and JRadioButton components that 
 * it contains to the same color./*from w  w  w .java  2 s. c om*/
 * 
 * @param buttons the button group to set the background for.
 * @param bg the background color.
 */
public static void setBackground(ButtonGroup buttons, Color bg) {
    Enumeration<?> children = buttons.getElements();
    if (children == null) {
        return;
    }
    Component child;

    if (bg != null) {
        while (children.hasMoreElements()) {
            child = (Component) children.nextElement();
            if (!bg.equals(child.getBackground())
                    && ((child instanceof JCheckBox) || (child instanceof JComboBox)
                            || (child instanceof JButton) || (child instanceof JRadioButton))) {
                child.setBackground(bg);
            }
        }
    }
}