Example usage for javax.swing ButtonGroup getSelection

List of usage examples for javax.swing ButtonGroup getSelection

Introduction

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

Prototype

public ButtonModel getSelection() 

Source Link

Document

Returns the model of the selected button.

Usage

From source file:Main.java

public static void main(String[] args) {
    JRadioButton dem = new JRadioButton("Bill", false);
    dem.setActionCommand("Bill");
    JRadioButton rep = new JRadioButton("Bob", false);
    rep.setActionCommand("Bob");
    JRadioButton ind = new JRadioButton("Ross", false);
    ind.setActionCommand("Ross");

    final ButtonGroup group = new ButtonGroup();
    group.add(dem);/*w ww .j a v a2  s  .  c  o  m*/
    group.add(rep);
    group.add(ind);

    class VoteActionListener implements ActionListener {
        public void actionPerformed(ActionEvent ex) {
            String choice = group.getSelection().getActionCommand();
            System.out.println("ACTION Candidate Selected: " + choice);
        }
    }

    class VoteItemListener implements ItemListener {
        public void itemStateChanged(ItemEvent ex) {
            String item = ((AbstractButton) ex.getItemSelectable()).getActionCommand();
            boolean selected = (ex.getStateChange() == ItemEvent.SELECTED);
            System.out.println("ITEM Candidate Selected: " + selected + " Selection: " + item);
        }
    }

    ActionListener al = new VoteActionListener();
    dem.addActionListener(al);
    rep.addActionListener(al);
    ind.addActionListener(al);

    ItemListener il = new VoteItemListener();
    dem.addItemListener(il);
    rep.addItemListener(il);
    ind.addItemListener(il);

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container c = frame.getContentPane();
    c.setLayout(new GridLayout(4, 1));
    c.add(new JLabel("Please Cast Your Vote"));
    c.add(dem);
    c.add(rep);
    c.add(ind);
    frame.pack();
    frame.setVisible(true);
}

From source file:SimpleButtonGroupExample.java

public static void main(String[] args) {
    // Some choices
    JRadioButton choice1, choice2, choice3;
    choice1 = new JRadioButton("Bach: Well Tempered Clavier, Book I");
    choice1.setActionCommand("bach1");
    choice2 = new JRadioButton("Bach: Well Tempered Clavier, Book II");
    choice2.setActionCommand("bach2");
    choice3 = new JRadioButton("Shostakovich: 24 Preludes and Fugues");
    choice3.setActionCommand("shostakovich");

    // A group, to ensure that we only vote for one.
    final ButtonGroup group = new ButtonGroup();
    group.add(choice1);/*from  w w w  .jav a 2 s  . c o m*/
    group.add(choice2);
    group.add(choice3);

    // A simple ActionListener, showing each selection using the ButtonModel
    class VoteActionListener implements ActionListener {
        public void actionPerformed(ActionEvent ev) {
            String choice = group.getSelection().getActionCommand();
            System.out.println("ACTION Choice Selected: " + choice);
        }
    }

    // A simple ItemListener, showing each selection and deselection
    class VoteItemListener implements ItemListener {
        public void itemStateChanged(ItemEvent ev) {
            boolean selected = (ev.getStateChange() == ItemEvent.SELECTED);
            AbstractButton button = (AbstractButton) ev.getItemSelectable();
            System.out
                    .println("ITEM Choice Selected: " + selected + ", Selection: " + button.getActionCommand());
        }
    }

    // Add listeners to each button
    ActionListener alisten = new VoteActionListener();
    choice1.addActionListener(alisten);
    choice2.addActionListener(alisten);
    choice3.addActionListener(alisten);

    ItemListener ilisten = new VoteItemListener();
    choice1.addItemListener(ilisten);
    choice2.addItemListener(ilisten);
    choice3.addItemListener(ilisten);

    // Throw everything together
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container c = frame.getContentPane();
    c.setLayout(new GridLayout(0, 1));
    c.add(new JLabel("Vote for your favorite prelude & fugue cycle"));
    c.add(choice1);
    c.add(choice2);
    c.add(choice3);
    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

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

    JPanel entreePanel = new JPanel();
    final ButtonGroup entreeGroup = new ButtonGroup();
    JRadioButton radioButton;//  ww  w . ja v a  2s .c  o  m
    entreePanel.add(radioButton = new JRadioButton("A"));
    radioButton.setActionCommand("A");
    entreeGroup.add(radioButton);
    entreePanel.add(radioButton = new JRadioButton("B"));
    radioButton.setActionCommand("B");
    entreeGroup.add(radioButton);
    entreePanel.add(radioButton = new JRadioButton("C", true));
    radioButton.setActionCommand("C");
    entreeGroup.add(radioButton);

    final JPanel condimentsPanel = new JPanel();
    condimentsPanel.add(new JCheckBox("Ketchup"));
    condimentsPanel.add(new JCheckBox("Mustard"));
    condimentsPanel.add(new JCheckBox("Pickles"));

    JPanel orderPanel = new JPanel();
    JButton orderButton = new JButton("Place Order");
    orderPanel.add(orderButton);

    frame.setLayout(new GridLayout(3, 1));
    frame.add(entreePanel);
    frame.add(condimentsPanel);
    frame.add(orderPanel);

    orderButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            String entree = entreeGroup.getSelection().getActionCommand();
            System.out.println(entree + " sandwich");
            Component[] components = condimentsPanel.getComponents();
            for (Component c : components) {
                JCheckBox cb = (JCheckBox) c;
                if (cb.isSelected())
                    System.out.println("With " + cb.getText());
            }
        }
    });

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(300, 150);
    frame.setVisible(true);
}

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;
        }//  ww  w . ja  v a 2 s.co  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  ww .ja  v a2 s . c o m
 * @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:layout.FlowLayoutDemo.java

public void addComponentsToPane(final Container pane) {
    final JPanel compsToExperiment = new JPanel();
    compsToExperiment.setLayout(experimentLayout);
    experimentLayout.setAlignment(FlowLayout.TRAILING);
    JPanel controls = new JPanel();
    controls.setLayout(new FlowLayout());

    LtoRbutton = new JRadioButton(LtoR);
    LtoRbutton.setActionCommand(LtoR);//from   w  ww . j  a  va 2s .co  m
    LtoRbutton.setSelected(true);
    RtoLbutton = new JRadioButton(RtoL);
    RtoLbutton.setActionCommand(RtoL);

    //Add buttons to the experiment layout
    compsToExperiment.add(new JButton("Button 1"));
    compsToExperiment.add(new JButton("Button 2"));
    compsToExperiment.add(new JButton("Button 3"));
    compsToExperiment.add(new JButton("Long-Named Button 4"));
    compsToExperiment.add(new JButton("5"));
    //Left to right component orientation is selected by default
    compsToExperiment.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);

    //Add controls to set up the component orientation in the experiment layout
    final ButtonGroup group = new ButtonGroup();
    group.add(LtoRbutton);
    group.add(RtoLbutton);
    controls.add(LtoRbutton);
    controls.add(RtoLbutton);
    controls.add(applyButton);

    //Process the Apply component orientation button press
    applyButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            String command = group.getSelection().getActionCommand();
            //Check the selection
            if (command.equals("Left to right")) {
                compsToExperiment.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
            } else {
                compsToExperiment.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
            }
            //update the experiment layout
            compsToExperiment.validate();
            compsToExperiment.repaint();
        }
    });
    pane.add(compsToExperiment, BorderLayout.CENTER);
    pane.add(controls, BorderLayout.SOUTH);
    ;
}

From source file:view.transacao.Relatorio.java

public JRadioButton getSelectedRadio(ButtonGroup group) {
    for (Enumeration eRadio = group.getElements(); eRadio.hasMoreElements();) {
        JRadioButton radioButton = (JRadioButton) eRadio.nextElement();

        if (radioButton.getModel() == group.getSelection()) {
            return radioButton;
        }/*from w  w w.  jav a  2  s.  com*/
    }

    return null;
}

From source file:VoteDialog.java

private JPanel createSimpleDialogBox() {
    final int numButtons = 4;
    JRadioButton[] radioButtons = new JRadioButton[numButtons];

    final ButtonGroup group = new ButtonGroup();

    JButton voteButton = null;//w  w w.j  a va2 s.  c  o  m

    final String defaultMessageCommand = "default";
    final String yesNoCommand = "yesno";
    final String yeahNahCommand = "yeahnah";
    final String yncCommand = "ync";

    radioButtons[0] = new JRadioButton("<html>Candidate 1: <font color=red>Sparky the Dog</font></html>");
    radioButtons[0].setActionCommand(defaultMessageCommand);

    radioButtons[1] = new JRadioButton("<html>Candidate 2: <font color=green>Shady Sadie</font></html>");
    radioButtons[1].setActionCommand(yesNoCommand);

    radioButtons[2] = new JRadioButton("<html>Candidate 3: <font color=blue>R.I.P. McDaniels</font></html>");
    radioButtons[2].setActionCommand(yeahNahCommand);

    radioButtons[3] = new JRadioButton(
            "<html>Candidate 4: <font color=maroon>Duke the Java<font size=-2><sup>TM</sup></font size> Platform Mascot</font></html>");
    radioButtons[3].setActionCommand(yncCommand);

    for (int i = 0; i < numButtons; i++) {
        group.add(radioButtons[i]);
    }
    // Select the first button by default.
    radioButtons[0].setSelected(true);

    voteButton = new JButton("Vote");

    voteButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            String command = group.getSelection().getActionCommand();

            // ok dialog
            if (command == defaultMessageCommand) {
                JOptionPane.showMessageDialog(frame, "This candidate is a dog. Invalid vote.");

                // yes/no dialog
            } else if (command == yesNoCommand) {
                int n = JOptionPane.showConfirmDialog(frame,
                        "This candidate is a convicted felon. \nDo you still want to vote for her?",
                        "A Follow-up Question", JOptionPane.YES_NO_OPTION);
                if (n == JOptionPane.YES_OPTION) {
                    setLabel("OK. Keep an eye on your wallet.");
                } else if (n == JOptionPane.NO_OPTION) {
                    setLabel("Whew! Good choice.");
                } else {
                    setLabel("It is your civic duty to cast your vote.");
                }

                // yes/no (with customized wording)
            } else if (command == yeahNahCommand) {
                Object[] options = { "Yes, please", "No, thanks" };
                int n = JOptionPane.showOptionDialog(frame,
                        "This candidate is deceased. \nDo you still want to vote for him?",
                        "A Follow-up Question", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null,
                        options, options[0]);
                if (n == JOptionPane.YES_OPTION) {
                    setLabel("I hope you don't expect much from your candidate.");
                } else if (n == JOptionPane.NO_OPTION) {
                    setLabel("Whew! Good choice.");
                } else {
                    setLabel("It is your civic duty to cast your vote.");
                }

                // yes/no/cancel (with customized wording)
            } else if (command == yncCommand) {
                Object[] options = { "Yes!", "No, I'll pass", "Well, if I must" };
                int n = JOptionPane.showOptionDialog(frame,
                        "Duke is a cartoon mascot. \nDo you  " + "still want to cast your vote?",
                        "A Follow-up Question", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE,
                        null, options, options[2]);
                if (n == JOptionPane.YES_OPTION) {
                    setLabel("Excellent choice.");
                } else if (n == JOptionPane.NO_OPTION) {
                    setLabel("Whatever you say. It's your vote.");
                } else if (n == JOptionPane.CANCEL_OPTION) {
                    setLabel("Well, I'm certainly not going to make you vote.");
                } else {
                    setLabel("It is your civic duty to cast your vote.");
                }
            }
            return;
        }
    });
    System.out.println("calling createPane");
    return createPane(simpleDialogDesc + ":", radioButtons, voteButton);
}

From source file:components.DialogDemo.java

/** Creates the panel shown by the first tab. */
private JPanel createSimpleDialogBox() {
    final int numButtons = 4;
    JRadioButton[] radioButtons = new JRadioButton[numButtons];
    final ButtonGroup group = new ButtonGroup();

    JButton showItButton = null;/*ww  w .j  a va 2  s.c  om*/

    final String defaultMessageCommand = "default";
    final String yesNoCommand = "yesno";
    final String yeahNahCommand = "yeahnah";
    final String yncCommand = "ync";

    radioButtons[0] = new JRadioButton("OK (in the L&F's words)");
    radioButtons[0].setActionCommand(defaultMessageCommand);

    radioButtons[1] = new JRadioButton("Yes/No (in the L&F's words)");
    radioButtons[1].setActionCommand(yesNoCommand);

    radioButtons[2] = new JRadioButton("Yes/No " + "(in the programmer's words)");
    radioButtons[2].setActionCommand(yeahNahCommand);

    radioButtons[3] = new JRadioButton("Yes/No/Cancel " + "(in the programmer's words)");
    radioButtons[3].setActionCommand(yncCommand);

    for (int i = 0; i < numButtons; i++) {
        group.add(radioButtons[i]);
    }
    radioButtons[0].setSelected(true);

    showItButton = new JButton("Show it!");
    showItButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            String command = group.getSelection().getActionCommand();

            //ok dialog
            if (command == defaultMessageCommand) {
                JOptionPane.showMessageDialog(frame, "Eggs aren't supposed to be green.");

                //yes/no dialog
            } else if (command == yesNoCommand) {
                int n = JOptionPane.showConfirmDialog(frame, "Would you like green eggs and ham?",
                        "An Inane Question", JOptionPane.YES_NO_OPTION);
                if (n == JOptionPane.YES_OPTION) {
                    setLabel("Ewww!");
                } else if (n == JOptionPane.NO_OPTION) {
                    setLabel("Me neither!");
                } else {
                    setLabel("Come on -- tell me!");
                }

                //yes/no (not in those words)
            } else if (command == yeahNahCommand) {
                Object[] options = { "Yes, please", "No way!" };
                int n = JOptionPane.showOptionDialog(frame, "Would you like green eggs and ham?",
                        "A Silly Question", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null,
                        options, options[0]);
                if (n == JOptionPane.YES_OPTION) {
                    setLabel("You're kidding!");
                } else if (n == JOptionPane.NO_OPTION) {
                    setLabel("I don't like them, either.");
                } else {
                    setLabel("Come on -- 'fess up!");
                }

                //yes/no/cancel (not in those words)
            } else if (command == yncCommand) {
                Object[] options = { "Yes, please", "No, thanks", "No eggs, no ham!" };
                int n = JOptionPane.showOptionDialog(frame,
                        "Would you like some green eggs to go " + "with that ham?", "A Silly Question",
                        JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, options,
                        options[2]);
                if (n == JOptionPane.YES_OPTION) {
                    setLabel("Here you go: green eggs and ham!");
                } else if (n == JOptionPane.NO_OPTION) {
                    setLabel("OK, just the ham, then.");
                } else if (n == JOptionPane.CANCEL_OPTION) {
                    setLabel("Well, I'm certainly not going to eat them!");
                } else {
                    setLabel("Please tell me what you want!");
                }
            }
            return;
        }
    });

    return createPane(simpleDialogDesc + ":", radioButtons, showItButton);
}

From source file:DialogDemo.java

/** Creates the panel shown by the first tab. */
private JPanel createSimpleDialogBox() {
    final int numButtons = 4;
    JRadioButton[] radioButtons = new JRadioButton[numButtons];
    final ButtonGroup group = new ButtonGroup();

    JButton showItButton = null;//from  w  w  w.j  ava 2 s  .c o m

    final String defaultMessageCommand = "default";
    final String yesNoCommand = "yesno";
    final String yeahNahCommand = "yeahnah";
    final String yncCommand = "ync";

    radioButtons[0] = new JRadioButton("OK (in the L&F's words)");
    radioButtons[0].setActionCommand(defaultMessageCommand);

    radioButtons[1] = new JRadioButton("Yes/No (in the L&F's words)");
    radioButtons[1].setActionCommand(yesNoCommand);

    radioButtons[2] = new JRadioButton("Yes/No " + "(in the programmer's words)");
    radioButtons[2].setActionCommand(yeahNahCommand);

    radioButtons[3] = new JRadioButton("Yes/No/Cancel " + "(in the programmer's words)");
    radioButtons[3].setActionCommand(yncCommand);

    for (int i = 0; i < numButtons; i++) {
        group.add(radioButtons[i]);
    }
    radioButtons[0].setSelected(true);

    showItButton = new JButton("Show it!");
    showItButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            String command = group.getSelection().getActionCommand();

            // ok dialog
            if (command == defaultMessageCommand) {
                JOptionPane.showMessageDialog(frame, "Eggs aren't supposed to be green.");

                // yes/no dialog
            } else if (command == yesNoCommand) {
                int n = JOptionPane.showConfirmDialog(frame, "Would you like green eggs and ham?",
                        "An Inane Question", JOptionPane.YES_NO_OPTION);
                if (n == JOptionPane.YES_OPTION) {
                    setLabel("Ewww!");
                } else if (n == JOptionPane.NO_OPTION) {
                    setLabel("Me neither!");
                } else {
                    setLabel("Come on -- tell me!");
                }

                // yes/no (not in those words)
            } else if (command == yeahNahCommand) {
                Object[] options = { "Yes, please", "No way!" };
                int n = JOptionPane.showOptionDialog(frame, "Would you like green eggs and ham?",
                        "A Silly Question", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null,
                        options, options[0]);
                if (n == JOptionPane.YES_OPTION) {
                    setLabel("You're kidding!");
                } else if (n == JOptionPane.NO_OPTION) {
                    setLabel("I don't like them, either.");
                } else {
                    setLabel("Come on -- 'fess up!");
                }

                // yes/no/cancel (not in those words)
            } else if (command == yncCommand) {
                Object[] options = { "Yes, please", "No, thanks", "No eggs, no ham!" };
                int n = JOptionPane.showOptionDialog(frame,
                        "Would you like some green eggs to go " + "with that ham?", "A Silly Question",
                        JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, options,
                        options[2]);
                if (n == JOptionPane.YES_OPTION) {
                    setLabel("Here you go: green eggs and ham!");
                } else if (n == JOptionPane.NO_OPTION) {
                    setLabel("OK, just the ham, then.");
                } else if (n == JOptionPane.CANCEL_OPTION) {
                    setLabel("Well, I'm certainly not going to eat them!");
                } else {
                    setLabel("Please tell me what you want!");
                }
            }
            return;
        }
    });

    return createPane(simpleDialogDesc + ":", radioButtons, showItButton);
}