Example usage for javax.swing JRadioButton setSelected

List of usage examples for javax.swing JRadioButton setSelected

Introduction

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

Prototype

public void setSelected(boolean b) 

Source Link

Document

Sets the state of the button.

Usage

From source file:JRadioButtonTest.java

public static void main(String[] args) {
    JFrame.setDefaultLookAndFeelDecorated(true);
    JFrame frame = new JFrame("JRadioButton Test");
    frame.setLayout(new FlowLayout());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JRadioButton button1 = new JRadioButton("Red");
    JRadioButton button2 = new JRadioButton("Green");
    JRadioButton button3 = new JRadioButton("Blue");
    ButtonGroup colorButtonGroup = new ButtonGroup();
    colorButtonGroup.add(button1);/*from  ww  w.  j  ava2 s  .  c o m*/
    colorButtonGroup.add(button2);
    colorButtonGroup.add(button3);
    button1.setSelected(true);
    frame.add(new JLabel("Color:"));
    frame.add(button1);
    frame.add(button2);

    frame.add(button3);
    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {

    JRadioButton button1 = new JRadioButton("Red");
    JRadioButton button2 = new JRadioButton("Green");
    JRadioButton button3 = new JRadioButton("Blue");
    ButtonGroup colorButtonGroup = new ButtonGroup();
    colorButtonGroup.add(button1);/*w w  w .  jav a 2s  .c  o m*/
    colorButtonGroup.add(button2);
    colorButtonGroup.add(button3);
    button1.setSelected(true);

    JFrame frame = new JFrame();
    frame.setLayout(new FlowLayout());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.add(new JLabel("Color:"));
    frame.add(button1);
    frame.add(button2);
    frame.add(button3);
    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {

    JRadioButton button1 = new JRadioButton("Red");
    JRadioButton button2 = new JRadioButton("Green");
    JRadioButton button3 = new JRadioButton("Blue");
    ButtonGroup colorButtonGroup = new ButtonGroup();
    colorButtonGroup.add(button1);/*ww w.j  ava  2 s  .  c o  m*/
    colorButtonGroup.add(button2);
    colorButtonGroup.add(button3);
    button1.setSelected(true);

    JFrame frame = new JFrame();
    frame.setLayout(new FlowLayout());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.add(new JLabel("Color:"));
    frame.add(button1);
    frame.add(button2);
    frame.add(button3);
    frame.pack();
    frame.setVisible(true);

    System.out.println(getSelection(colorButtonGroup));

}

From source file:Main.java

public static void main(String[] args) {

    JRadioButton button1 = new JRadioButton("Red");
    JRadioButton button2 = new JRadioButton("Green");

    button2.setMnemonic(KeyEvent.VK_G);

    JRadioButton button3 = new JRadioButton("Blue");
    ButtonGroup colorButtonGroup = new ButtonGroup();
    colorButtonGroup.add(button1);/*  ww w.  j  av a2 s . com*/
    colorButtonGroup.add(button2);
    colorButtonGroup.add(button3);
    button1.setSelected(true);

    JFrame frame = new JFrame();
    frame.setLayout(new FlowLayout());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.add(new JLabel("Color:"));
    frame.add(button1);
    frame.add(button2);
    frame.add(button3);
    frame.pack();
    frame.setVisible(true);

}

From source file:Main.java

/**
 * Creates a radio button with all the properties set up front.
 * /*from ww  w  .ja v a 2  s  .  c  o  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);

    JRadioButton rb2 = new JRadioButton("Female");
    rb2.setMnemonic(KeyEvent.VK_F);
    rb2.setActionCommand("Female");
    rb2.setSelected(true);/*from ww  w.ja  v a2  s  .c om*/
    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:ListTest.java

/**
 * Makes a radio button to set the layout orientation.
 * @param label the button label// w w  w . j  a  v  a2  s . c  o  m
 * @param orientation the orientation for the list
 */
private void makeButton(String label, final int orientation) {
    JRadioButton button = new JRadioButton(label);
    buttonPanel.add(button);
    if (group.getButtonCount() == 0)
        button.setSelected(true);
    group.add(button);
    button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            wordList.setLayoutOrientation(orientation);
            listPanel.revalidate();
        }
    });
}

From source file:com.jdom.util.patterns.mvp.swing.RadioButtonGroupDialog.java

private RadioButtonGroupDialog(Frame frame, Component locationComp, String labelText, String title,
        Collection<String> data, String initialValue, String longValue) {
    super(frame, title, true);

    final JButton cancelButton = new JButton("Cancel");
    cancelButton.addActionListener(this);

    final JButton setButton = new JButton("OK");
    setButton.setActionCommand("OK");
    setButton.addActionListener(this);
    getRootPane().setDefaultButton(setButton);

    ButtonGroup radioButtonGroup = new ButtonGroup();
    for (String option : data) {
        JRadioButton button = new JRadioButton(option);
        if (option.equals(initialValue)) {
            button.setSelected(true);
        }//from  w  ww .  jav  a 2  s.  co m
        radioButtonGroup.add(button);
        panel.add(button);
    }

    JScrollPane listScroller = new JScrollPane(panel);
    listScroller.setPreferredSize(new Dimension(250, 80));
    listScroller.setAlignmentX(LEFT_ALIGNMENT);

    JPanel listPane = new JPanel();
    listPane.setLayout(new BoxLayout(listPane, BoxLayout.PAGE_AXIS));

    if (!StringUtils.isEmpty(labelText)) {
        JLabel label = new JLabel(labelText);
        label.setText(labelText);
        listPane.add(label);
    }
    listPane.add(Box.createRigidArea(new Dimension(0, 5)));
    listPane.add(listScroller);
    listPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

    JPanel buttonPane = new JPanel();
    buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.LINE_AXIS));
    buttonPane.setBorder(BorderFactory.createEmptyBorder(0, 10, 10, 10));
    buttonPane.add(Box.createHorizontalGlue());
    buttonPane.add(cancelButton);
    buttonPane.add(Box.createRigidArea(new Dimension(10, 0)));
    buttonPane.add(setButton);

    Container contentPane = getContentPane();
    contentPane.add(listPane, BorderLayout.CENTER);
    contentPane.add(buttonPane, BorderLayout.PAGE_END);

    pack();
    setLocationRelativeTo(locationComp);
}

From source file:QandE.LunarPhasesRB.java

private void addWidgets() {
    /*//from   ww  w .  jav a  2 s.  com
     * 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:OptionDialogTest.java

/**
 * Constructs a button panel./* w w w  .jav a 2s.c o  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]);
    }
}