Example usage for javax.swing ButtonGroup ButtonGroup

List of usage examples for javax.swing ButtonGroup ButtonGroup

Introduction

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

Prototype

public ButtonGroup() 

Source Link

Document

Creates a new ButtonGroup.

Usage

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);/*from w  ww  . j a v  a  2  s  .c  om*/

    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:Main.java

public Main() {
    rOne.addActionListener(new ROneAction());
    rTwo.addActionListener(new RTwoAction());
    group = new ButtonGroup();
    group.add(rOne);/*  w w w  .ja v  a 2 s  .co  m*/
    group.add(rTwo);

    combo = new JComboBox();
    combo.addItem("No Values");
    combo.addPopupMenuListener(new PopupMenuListener() {
        public void popupMenuCanceled(PopupMenuEvent evt) {
        }

        public void popupMenuWillBecomeInvisible(PopupMenuEvent evt) {
            jComboBox1PopupMenuWillBecomeInvisible(evt);
        }

        public void popupMenuWillBecomeVisible(PopupMenuEvent evt) {
        }
    });

    label = new JLabel("labelLabel");

    this.setLayout(new FlowLayout());
    this.add(rOne);
    this.add(rTwo);
    this.add(combo);
    this.add(label);
    this.pack();
    this.setVisible(true);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

From source file:Main.java

public Main(String title) {
    super(title);
    this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    Container contentPane = this.getContentPane();
    ButtonGroup group = new ButtonGroup();
    small = new JRadioButton("small");
    medium = new JRadioButton("medium");
    large = new JRadioButton("large");

    group.add(small);//from   w ww.  j  ava  2 s.c o m
    group.add(medium);
    group.add(large);
    button = new JButton("Click here.");
    button.setBounds(100, 50, 100, 50);
    JPanel center = new JPanel();
    center.setLayout(null);
    center.add(button);
    contentPane.add(center, BorderLayout.CENTER);

    JPanel north = new JPanel();
    north.add(small);
    north.add(medium);
    north.add(large);
    contentPane.add(north, BorderLayout.NORTH);

    ChangeSize listener = new ChangeSize(button);
    small.addItemListener(listener);
    medium.addItemListener(listener);
    large.addItemListener(listener);
}

From source file:Main.java

public Main() {
    JRadioButton radMarriedYes = new JRadioButton(new AbstractAction("Yes") {

        @Override//from   w ww.  jav  a 2 s.  co  m
        public void actionPerformed(ActionEvent arg0) {

        }

    });
    JRadioButton radMarriedNo = new JRadioButton("No?", false);
    JRadioButton radGolfYes = new JRadioButton("Yes?", false);
    JRadioButton radGolfNo = new JRadioButton("No?", true);

    ButtonGroup radioGroup1 = new ButtonGroup();
    ButtonGroup radioGroup2 = new ButtonGroup();

    setLayout(null);

    add(radMarriedYes);
    add(radMarriedNo);
    add(radGolfYes);
    add(radGolfNo);

    radioGroup1.add(radMarriedYes);
    radioGroup1.add(radMarriedNo);
    radioGroup2.add(radGolfYes);
    radioGroup2.add(radGolfNo);

    radMarriedYes.setBounds(30, 50, 50, 20);
    radMarriedNo.setBounds(30, 80, 50, 20);

    radGolfYes.setBounds(150, 50, 50, 20);
    radGolfNo.setBounds(150, 80, 50, 20);

}

From source file:Main.java

/**
 * Groups all buttons inside this container and all subcontainers if
 * requested and returns created button group.
 *
 * @param container//  w  ww. j  av a2 s  .  c o  m
 *            container to process
 * @param recursive
 *            whether to check all subcontainers or not
 * @return created button group
 */
public static ButtonGroup groupButtons(final Container container, final boolean recursive) {
    final ButtonGroup buttonGroup = new ButtonGroup();
    groupButtons(container, recursive, buttonGroup);
    return buttonGroup;
}

From source file:MainClass.java

public MainClass() {
    theFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    cp = theFrame.getContentPane();/*www  . j  a v  a2 s . c om*/
    cp.setLayout(new FlowLayout());

    ButtonGroup bg = new ButtonGroup();

    JRadioButton bJava = new JRadioButton("Java");
    bJava.addActionListener(new LNFSetter("javax.swing.plaf.metal.MetalLookAndFeel", bJava));
    bg.add(bJava);
    cp.add(bJava);

    JRadioButton bMSW = new JRadioButton("MS-Windows");
    bMSW.addActionListener(new LNFSetter("com.sun.java.swing.plaf.windows.WindowsLookAndFeel", bMSW));
    bg.add(bMSW);
    cp.add(bMSW);

    JRadioButton bMotif = new JRadioButton("Motif");
    bMotif.addActionListener(new LNFSetter("com.sun.java.swing.plaf.motif.MotifLookAndFeel", bMotif));
    bg.add(bMotif);
    cp.add(bMotif);

    JRadioButton bMac = new JRadioButton("Sun-MacOS");
    bMac.addActionListener(new LNFSetter("com.sun.java.swing.plaf.mac.MacLookAndFeel", bMac));
    bg.add(bMac);
    cp.add(bMac);

    String defaultLookAndFeel = UIManager.getSystemLookAndFeelClassName();
    JRadioButton bDefault = new JRadioButton("Default");
    bDefault.addActionListener(new LNFSetter(defaultLookAndFeel, bDefault));
    bg.add(bDefault);
    cp.add(bDefault);

    (previousButton = bDefault).setSelected(true);

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

From source file:SimpleDraw.java

public SimpleDraw() {
    this.setTitle("Simple DRAW");
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    // add check box group
    ButtonGroup cbg = new ButtonGroup();
    JRadioButton lineButton = new JRadioButton("Line");
    JRadioButton ovalButton = new JRadioButton("Oval");
    JRadioButton rectangleButton = new JRadioButton("Rectangle");
    cbg.add(lineButton);/*from   w  w  w . ja  v a 2 s .  com*/
    cbg.add(ovalButton);
    cbg.add(rectangleButton);
    lineButton.addActionListener(this);
    ovalButton.addActionListener(this);
    rectangleButton.addActionListener(this);
    rectangleButton.setSelected(true);
    JPanel radioPanel = new JPanel(new FlowLayout());
    radioPanel.add(lineButton);
    radioPanel.add(ovalButton);
    radioPanel.add(rectangleButton);
    this.addMouseListener(this);
    this.setLayout(new BorderLayout());
    this.add(radioPanel, BorderLayout.NORTH);
}

From source file:Main.java

protected JMenuBar createMenuBar() {
    final JMenuBar menuBar = new JMenuBar();

    JMenu mFont = new JMenu("Font");
    mFont.setMnemonic('o');

    ButtonGroup group = new ButtonGroup();
    fontMenus = new JMenuItem[FontName.length];
    for (int k = 0; k < FontName.length; k++) {
        int m = k + 1;
        fontMenus[k] = new JRadioButtonMenuItem(m + " " + FontName[k]);
        boolean selected = (k == 0);
        fontMenus[k].setSelected(selected);
        fontMenus[k].setMnemonic('1' + k);
        fontMenus[k].setFont(fonts[k]);//from   ww w  .  j  av a2 s  . c o  m
        fontMenus[k].addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {
                System.out.println(((JComponent) arg0.getSource()).getFont());

            }

        });
        group.add(fontMenus[k]);
        mFont.add(fontMenus[k]);
    }

    menuBar.add(mFont);

    return menuBar;
}

From source file:CardLayoutDemo.java

public void addCardsToPane(Container pane) {
    JRadioButton[] rb = new JRadioButton[strings.length];
    ButtonGroup group = new ButtonGroup();
    JPanel buttons = new JPanel();
    buttons.setLayout(new BoxLayout(buttons, BoxLayout.PAGE_AXIS));

    for (int i = 0; i < strings.length; i++) {
        rb[i] = new JRadioButton("Show component #" + (i + 1));
        rb[i].setActionCommand(String.valueOf(i));
        rb[i].addActionListener(this);
        group.add(rb[i]);/*www . ja va 2  s  . c om*/
        buttons.add(rb[i]);
    }
    rb[0].setSelected(true);

    //Create the panel that contains the "cards".
    cards = new JPanel(new CardLayout());
    for (int i = 0; i < strings.length; i++) {
        cards.add(createComponent(strings[i]), String.valueOf(i));
    }

    pane.add(buttons, BorderLayout.NORTH);
    pane.add(cards, BorderLayout.CENTER);
}

From source file:ScrollDemo.java

public void init() {
    JRadioButton form[][] = new JRadioButton[12][5];
    String counts[] = { "", "0-1", "2-5", "6-10", "11-100", "101+" };
    String categories[] = { "Household", "Office", "Extended Family", "Company (US)", "Company (World)", "Team",
            "Will", "Birthday Card List", "High School", "Country", "Continent", "Planet" };
    JPanel p = new JPanel();
    p.setSize(600, 400);// www  .  j  a va 2s  .c  o  m
    p.setLayout(new GridLayout(13, 6, 10, 0));
    for (int row = 0; row < 13; row++) {
        ButtonGroup bg = new ButtonGroup();
        for (int col = 0; col < 6; col++) {
            if (row == 0) {
                p.add(new JLabel(counts[col]));
            } else {
                if (col == 0) {
                    p.add(new JLabel(categories[row - 1]));
                } else {
                    form[row - 1][col - 1] = new JRadioButton();
                    bg.add(form[row - 1][col - 1]);
                    p.add(form[row - 1][col - 1]);
                }
            }
        }
    }
    scrollpane = new JScrollPane(p);
    getContentPane().add(scrollpane, BorderLayout.CENTER);
}