Example usage for javax.swing JRadioButton setActionCommand

List of usage examples for javax.swing JRadioButton setActionCommand

Introduction

In this page you can find the example usage for javax.swing JRadioButton setActionCommand.

Prototype

public void setActionCommand(String actionCommand) 

Source Link

Document

Sets the action command for this 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);//from  w  w  w  .  ja  v  a 2 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:Main.java

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

    JPanel entreePanel = new JPanel();
    final ButtonGroup entreeGroup = new ButtonGroup();
    JRadioButton radioButton;
    entreePanel.add(radioButton = new JRadioButton("A"));
    radioButton.setActionCommand("A");
    entreeGroup.add(radioButton);//from  ww w .  j a  va2  s . c om
    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: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);/* w ww . j a  va 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

/**
 * Creates a radio button with all the properties set up front.
 * //from w w w  .  j  a v  a2s .  co m
 * @param name
 *            The caption of the button.
 * @param command
 *            The name of the command the listener should look for.
 * @param isSelected
 *            The condition that should be true if this control is selected.
 * @param listener
 *            The object listening for commands.
 * @return A configured radio button.
 */
public static JRadioButton createRadioButton(String name, String command, boolean isSelected,
        ActionListener listener) {
    JRadioButton button;
    button = new JRadioButton(name);
    button.setActionCommand(command);
    button.setSelected(isSelected);
    button.addActionListener(listener);
    return button;
}

From source file:MainClass.java

MainClass(String title) {
    super(title);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JRadioButton rb1 = new JRadioButton("Male");
    rb1.setMnemonic(KeyEvent.VK_M);
    rb1.setActionCommand("Male");
    rb1.setSelected(true);/*w ww  . j a v a2 s  . c  o  m*/

    JRadioButton rb2 = new JRadioButton("Female");
    rb2.setMnemonic(KeyEvent.VK_F);
    rb2.setActionCommand("Female");
    rb2.setSelected(true);
    ButtonGroup bg = new ButtonGroup();
    bg.add(rb1);
    bg.add(rb2);

    JPanel jp = new JPanel();
    jp.add(rb1);
    jp.add(rb2);

    getContentPane().add(jp);

    pack();
    setVisible(true);
}

From source file:OptionDialogTest.java

/**
 * Constructs a button panel./*w  w w  .j  a v  a 2s  .  co  m*/
 * @param title the title shown in the border
 * @param options an array of radio button labels
 */
public ButtonPanel(String title, String... options) {
    setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), title));
    setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
    group = new ButtonGroup();

    // make one radio button for each option
    for (String option : options) {
        JRadioButton b = new JRadioButton(option);
        b.setActionCommand(option);
        add(b);
        group.add(b);
        b.setSelected(option == options[0]);
    }
}

From source file:RadioButtonDemo.java

public RadioButtonDemo() {
    super(new BorderLayout());

    //Create the radio buttons.
    JRadioButton birdButton = new JRadioButton(birdString);
    birdButton.setMnemonic(KeyEvent.VK_B);
    birdButton.setActionCommand(birdString);
    birdButton.setSelected(true);/*from  w w w .j  a  v  a 2 s  .c  o m*/

    JRadioButton catButton = new JRadioButton(catString);
    catButton.setMnemonic(KeyEvent.VK_C);
    catButton.setActionCommand(catString);

    JRadioButton dogButton = new JRadioButton(dogString);
    dogButton.setMnemonic(KeyEvent.VK_D);
    dogButton.setActionCommand(dogString);

    JRadioButton rabbitButton = new JRadioButton(rabbitString);
    rabbitButton.setMnemonic(KeyEvent.VK_R);
    rabbitButton.setActionCommand(rabbitString);

    JRadioButton pigButton = new JRadioButton(pigString);
    pigButton.setMnemonic(KeyEvent.VK_P);
    pigButton.setActionCommand(pigString);

    //Group the radio buttons.
    ButtonGroup group = new ButtonGroup();
    group.add(birdButton);
    group.add(catButton);
    group.add(dogButton);
    group.add(rabbitButton);
    group.add(pigButton);

    //Register a listener for the radio buttons.
    birdButton.addActionListener(this);
    catButton.addActionListener(this);
    dogButton.addActionListener(this);
    rabbitButton.addActionListener(this);
    pigButton.addActionListener(this);

    //Set up the picture label.
    picture = new JLabel(createImageIcon("images/" + birdString + ".gif"));

    //The preferred size is hard-coded to be the width of the
    //widest image and the height of the tallest image.
    //A real program would compute this.
    picture.setPreferredSize(new Dimension(177, 122));

    //Put the radio buttons in a column in a panel.
    JPanel radioPanel = new JPanel(new GridLayout(0, 1));
    radioPanel.add(birdButton);
    radioPanel.add(catButton);
    radioPanel.add(dogButton);
    radioPanel.add(rabbitButton);
    radioPanel.add(pigButton);

    add(radioPanel, BorderLayout.LINE_START);
    add(picture, BorderLayout.CENTER);
    setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
}

From source file:QandE.LunarPhasesRB.java

private void addWidgets() {
    /*//from  ww  w .  j ava 2 s. c o  m
     * Create a label for displaying the moon phase images and
     * put a border around it.
     */
    phaseIconLabel = new JLabel();
    phaseIconLabel.setHorizontalAlignment(JLabel.CENTER);
    phaseIconLabel.setVerticalAlignment(JLabel.CENTER);
    phaseIconLabel.setVerticalTextPosition(JLabel.CENTER);
    phaseIconLabel.setHorizontalTextPosition(JLabel.CENTER);
    phaseIconLabel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createLoweredBevelBorder(),
            BorderFactory.createEmptyBorder(5, 5, 5, 5)));

    phaseIconLabel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEmptyBorder(0, 0, 10, 0),
            phaseIconLabel.getBorder()));

    //Create radio buttons with lunar phase choices.
    JRadioButton newButton = new JRadioButton("New");
    newButton.setActionCommand("0");
    newButton.setSelected(true);

    JRadioButton waxingCrescentButton = new JRadioButton("Waxing Crescent");
    waxingCrescentButton.setActionCommand("1");

    JRadioButton firstQuarterButton = new JRadioButton("First Quarter");
    firstQuarterButton.setActionCommand("2");

    JRadioButton waxingGibbousButton = new JRadioButton("Waxing Gibbous");
    waxingGibbousButton.setActionCommand("3");

    JRadioButton fullButton = new JRadioButton("Full");
    fullButton.setActionCommand("4");

    JRadioButton waningGibbousButton = new JRadioButton("Waning Gibbous");
    waningGibbousButton.setActionCommand("5");

    JRadioButton thirdQuarterButton = new JRadioButton("Third Quarter");
    thirdQuarterButton.setActionCommand("6");

    JRadioButton waningCrescentButton = new JRadioButton("Waning Crescent");
    waningCrescentButton.setActionCommand("7");

    // Create a button group and add the radio buttons.
    ButtonGroup group = new ButtonGroup();
    group.add(newButton);
    group.add(waxingCrescentButton);
    group.add(firstQuarterButton);
    group.add(waxingGibbousButton);
    group.add(fullButton);
    group.add(waningGibbousButton);
    group.add(thirdQuarterButton);
    group.add(waningCrescentButton);

    // Display the first image.
    phaseIconLabel.setIcon(new ImageIcon("images/image0.jpg"));
    phaseIconLabel.setText("");

    //Make the radio buttons appear in a center-aligned column.
    selectPanel.setLayout(new BoxLayout(selectPanel, BoxLayout.PAGE_AXIS));
    selectPanel.setAlignmentX(Component.CENTER_ALIGNMENT);

    //Add a border around the select panel.
    selectPanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder("Select Phase"),
            BorderFactory.createEmptyBorder(5, 5, 5, 5)));

    //Add a border around the display panel.
    displayPanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder("Display Phase"),
            BorderFactory.createEmptyBorder(5, 5, 5, 5)));

    //Add image and moon phases radio buttons to select panel.
    displayPanel.add(phaseIconLabel);
    selectPanel.add(newButton);
    selectPanel.add(waxingCrescentButton);
    selectPanel.add(firstQuarterButton);
    selectPanel.add(waxingGibbousButton);
    selectPanel.add(fullButton);
    selectPanel.add(waningGibbousButton);
    selectPanel.add(thirdQuarterButton);
    selectPanel.add(waningCrescentButton);

    //Listen to events from the radio buttons.
    newButton.addActionListener(this);
    waxingCrescentButton.addActionListener(this);
    firstQuarterButton.addActionListener(this);
    waxingGibbousButton.addActionListener(this);
    fullButton.addActionListener(this);
    waningGibbousButton.addActionListener(this);
    thirdQuarterButton.addActionListener(this);
    waningCrescentButton.addActionListener(this);

}

From source file:components.FrameDemo2.java

protected JComponent createOptionControls() {
    JLabel label1 = new JLabel("Decoration options for subsequently created frames:");
    ButtonGroup bg1 = new ButtonGroup();
    JLabel label2 = new JLabel("Icon options:");
    ButtonGroup bg2 = new ButtonGroup();

    //Create the buttons
    JRadioButton rb1 = new JRadioButton();
    rb1.setText("Look and feel decorated");
    rb1.setActionCommand(LF_DECORATIONS);
    rb1.addActionListener(this);
    rb1.setSelected(true);/*from  ww w .  j a v  a  2 s.  c om*/
    bg1.add(rb1);
    //
    JRadioButton rb2 = new JRadioButton();
    rb2.setText("Window system decorated");
    rb2.setActionCommand(WS_DECORATIONS);
    rb2.addActionListener(this);
    bg1.add(rb2);
    //
    JRadioButton rb3 = new JRadioButton();
    rb3.setText("No decorations");
    rb3.setActionCommand(NO_DECORATIONS);
    rb3.addActionListener(this);
    bg1.add(rb3);
    //
    //
    JRadioButton rb4 = new JRadioButton();
    rb4.setText("Default icon");
    rb4.setActionCommand(DEFAULT_ICON);
    rb4.addActionListener(this);
    rb4.setSelected(true);
    bg2.add(rb4);
    //
    JRadioButton rb5 = new JRadioButton();
    rb5.setText("Icon from a JPEG file");
    rb5.setActionCommand(FILE_ICON);
    rb5.addActionListener(this);
    bg2.add(rb5);
    //
    JRadioButton rb6 = new JRadioButton();
    rb6.setText("Painted icon");
    rb6.setActionCommand(PAINT_ICON);
    rb6.addActionListener(this);
    bg2.add(rb6);

    //Add everything to a container.
    Box box = Box.createVerticalBox();
    box.add(label1);
    box.add(Box.createVerticalStrut(5)); //spacer
    box.add(rb1);
    box.add(rb2);
    box.add(rb3);
    //
    box.add(Box.createVerticalStrut(15)); //spacer
    box.add(label2);
    box.add(Box.createVerticalStrut(5)); //spacer
    box.add(rb4);
    box.add(rb5);
    box.add(rb6);

    //Add some breathing room.
    box.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

    return box;
}

From source file:ui.results.ResultChartPanel.java

/**
 * Creates the choice panel, that is placed above the chart.
 * @return   the choice panel/*from   w w  w.j  a v  a2 s  . c o m*/
 */
protected JPanel createChoicePanel() {
    // The radiobuttons
    JRadioButton line = new JRadioButton("Line Chart", true);
    JRadioButton bar = new JRadioButton("Bar Chart", false);

    line.setActionCommand("line chart");
    bar.setActionCommand("bar chart");

    line.addActionListener(this);
    bar.addActionListener(this);

    // Let the radiobuttons form a group: i.e. only one can be selected.
    ButtonGroup choiceGroup = new ButtonGroup();
    choiceGroup.add(line);
    choiceGroup.add(bar);

    // Joining it all on the panel
    JPanel choicePanel = new JPanel();
    choicePanel.add(line);
    choicePanel.add(bar);

    return choicePanel;
}