Example usage for javax.swing ButtonGroup add

List of usage examples for javax.swing ButtonGroup add

Introduction

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

Prototype

public void add(AbstractButton b) 

Source Link

Document

Adds the button to the group.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Action action1 = new AbstractAction("RadioButton Label1") {
        public void actionPerformed(ActionEvent evt) {
        }/*  ww w  .j a  v  a  2  s  .c  o  m*/
    };
    Action action2 = new AbstractAction("RadioButton Label2") {
        public void actionPerformed(ActionEvent evt) {
        }
    };

    JRadioButton b1 = new JRadioButton(action1);
    JRadioButton b2 = new JRadioButton(action2);

    ButtonGroup group = new ButtonGroup();
    group.add(b1);
    group.add(b2);

}

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);
    colorButtonGroup.add(button2);/*w w  w .ja v a2 s  .  c  o m*/
    colorButtonGroup.add(button3);

    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: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);
    colorButtonGroup.add(button2);/*from  ww  w. j ava 2s. com*/
    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);
    colorButtonGroup.add(button2);/*w  w  w  . j  a  v  a2  s. c o m*/
    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");

    button2.setMnemonic(KeyEvent.VK_G);

    JRadioButton button3 = new JRadioButton("Blue");
    ButtonGroup colorButtonGroup = new ButtonGroup();
    colorButtonGroup.add(button1);
    colorButtonGroup.add(button2);/*  ww  w  .  java  2  s .co m*/
    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);
    colorButtonGroup.add(button2);//from  w ww .ja va2s. co  m
    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:TryBoxLayout.java

public static void main(String[] args) {
    JFrame aWindow = new JFrame("This is a Box Layout");
    aWindow.setBounds(30, 30, 300, 300);
    aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    // Create left column of radio buttons
    Box left = Box.createVerticalBox();
    ButtonGroup radioGroup = new ButtonGroup();
    JRadioButton rbutton;//w w w .  j  a va  2s .c o m
    radioGroup.add(rbutton = new JRadioButton("Red"));
    left.add(rbutton);
    radioGroup.add(rbutton = new JRadioButton("Green"));
    left.add(rbutton);
    radioGroup.add(rbutton = new JRadioButton("Blue"));
    left.add(rbutton);
    radioGroup.add(rbutton = new JRadioButton("Yellow"));
    left.add(rbutton);
    Box right = Box.createVerticalBox();
    right.add(new JCheckBox("A"));
    right.add(new JCheckBox("B"));
    right.add(new JCheckBox("C"));
    Box top = Box.createHorizontalBox();
    top.add(left);
    top.add(right);
    Container content = aWindow.getContentPane();
    content.setLayout(new BorderLayout());
    content.add(top, BorderLayout.CENTER);
    aWindow.pack();
    aWindow.setVisible(true);
}

From source file:Main.java

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

    JPanel northPanel = new JPanel(new GridLayout(2, 1));

    JPanel welcomePanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
    welcomePanel.add(new JLabel("Welcome"));

    northPanel.add(welcomePanel);/*from  w w w.  j a  va  2 s.  c o m*/

    JPanel radioPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));

    JRadioButton button1 = new JRadioButton("Button 1", true);
    JRadioButton button2 = new JRadioButton("Button 2", false);

    ButtonGroup group = new ButtonGroup();
    group.add(button1);
    group.add(button2);

    radioPanel.add(button1);
    radioPanel.add(button2);

    northPanel.add(radioPanel);

    JPanel middlePanel = new JPanel(new GridLayout(3, 3));

    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            middlePanel.add(new JButton("Button " + i + j));
        }
    }

    JPanel southPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));

    southPanel.add(new JLabel("Whose turn:"));
    southPanel.add(new JButton("Reset"));

    frame.add(northPanel, BorderLayout.NORTH);
    frame.add(middlePanel, BorderLayout.CENTER);
    frame.add(southPanel, BorderLayout.SOUTH);

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

From source file:Main.java

public static void main(String args[]) {
    JFrame frame = new JFrame();
    Container contentPane = frame.getContentPane();
    contentPane.setLayout(new GridLayout(0, 1));
    ButtonGroup group = new ButtonGroup();
    JRadioButton option = new JRadioButton("French Fries", true);
    group.add(option);
    contentPane.add(option);/*  ww w  .j  a  va 2s  .  com*/
    option = new JRadioButton("Onion Rings", false);
    group.add(option);
    contentPane.add(option);
    option = new JRadioButton("Ice Cream", false);
    group.add(option);
    contentPane.add(option);
    frame.setSize(200, 200);
    frame.show();
}

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);
    group.add(rep);/*from w  w  w.j ava2s .com*/
    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);
}