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

public ClipArea() {
    super();//from  w  w w  .  jav  a2  s .  com
    Container contentPane = getContentPane();

    canvas = new MyCanvas();
    contentPane.add(canvas);

    JPanel panel = new JPanel();
    panel.setLayout(new GridLayout(1, 2));
    clipButton = new JRadioButton("Clip", true);
    clipButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            canvas.clip = true;
            canvas.clipFurther = false;
            canvas.repaint();
        }
    });
    clipFurButton = new JRadioButton("Clip Further");
    clipFurButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            canvas.clipFurther = true;
            canvas.repaint();
        }
    });
    ButtonGroup group = new ButtonGroup();
    group.add(clipButton);
    group.add(clipFurButton);

    panel.add(clipButton);
    panel.add(clipFurButton);
    contentPane.add(BorderLayout.SOUTH, panel);

    // 4. Add a window listener to close the frame properly.
    addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    });
    pack();
    setVisible(true);
}

From source file:MainClass.java

public MainClass() {
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    setJMenuBar(menuBar);//  w  w  w  .j a v  a2  s.c  om

    JMenu fileMenu = new JMenu("File");
    JMenu elementMenu = new JMenu("Elements");
    fileMenu.setMnemonic('F');
    elementMenu.setMnemonic('E');

    // Construct the file pull down menu
    newItem = fileMenu.add("New");
    openItem = fileMenu.add("Open");
    closeItem = fileMenu.add("Close");
    fileMenu.addSeparator();
    saveItem = fileMenu.add("Save");
    saveAsItem = fileMenu.add("Save As...");

    JMenu colorMenu = new JMenu("Color");

    fileMenu.addSeparator();
    printItem = fileMenu.add("Print");

    newItem.setAccelerator(KeyStroke.getKeyStroke('N', Event.CTRL_MASK));
    openItem.setAccelerator(KeyStroke.getKeyStroke('O', Event.CTRL_MASK));
    saveItem.setAccelerator(KeyStroke.getKeyStroke('S', Event.CTRL_MASK));
    printItem.setAccelerator(KeyStroke.getKeyStroke('P', Event.CTRL_MASK));

    elementMenu.add(lineItem = new JRadioButtonMenuItem("Line", true));
    elementMenu.add(rectangleItem = new JRadioButtonMenuItem("Rectangle", false));
    elementMenu.add(circleItem = new JRadioButtonMenuItem("Circle", false));
    elementMenu.add(curveItem = new JRadioButtonMenuItem("Curve", false));
    ButtonGroup types = new ButtonGroup();
    types.add(lineItem);
    types.add(rectangleItem);
    types.add(circleItem);
    types.add(curveItem);

    lineItem.setAccelerator(KeyStroke.getKeyStroke('L', Event.CTRL_MASK));
    rectangleItem.setAccelerator(KeyStroke.getKeyStroke('E', Event.CTRL_MASK));
    circleItem.setAccelerator(KeyStroke.getKeyStroke('I', Event.CTRL_MASK));
    curveItem.setAccelerator(KeyStroke.getKeyStroke('V', Event.CTRL_MASK));

    elementMenu.addSeparator();

    elementMenu.add(colorMenu); // Add the sub-menu
    colorMenu.add(redItem = new JCheckBoxMenuItem("Red", false));
    colorMenu.add(yellowItem = new JCheckBoxMenuItem("Yellow", false));
    colorMenu.add(greenItem = new JCheckBoxMenuItem("Green", false));
    colorMenu.add(blueItem = new JCheckBoxMenuItem("Blue", true));

    // Add element color accelerators
    redItem.setAccelerator(KeyStroke.getKeyStroke('R', Event.CTRL_MASK));
    yellowItem.setAccelerator(KeyStroke.getKeyStroke('Y', Event.CTRL_MASK));
    greenItem.setAccelerator(KeyStroke.getKeyStroke('G', Event.CTRL_MASK));
    blueItem.setAccelerator(KeyStroke.getKeyStroke('B', Event.CTRL_MASK));

    menuBar.add(fileMenu);
    menuBar.add(elementMenu);
}

From source file:Main.java

public Main() {
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    setJMenuBar(menuBar);/*from www  .ja v  a  2  s  . co  m*/

    JMenu fileMenu = new JMenu("File");
    JMenu elementMenu = new JMenu("Elements");
    fileMenu.setMnemonic('F');
    elementMenu.setMnemonic('E');

    // Construct the file pull down menu
    newItem = fileMenu.add("New");
    openItem = fileMenu.add("Open");
    closeItem = fileMenu.add("Close");
    fileMenu.addSeparator();
    saveItem = fileMenu.add("Save");
    saveAsItem = fileMenu.add("Save As...");

    JMenu colorMenu = new JMenu("Color");

    fileMenu.addSeparator();
    printItem = fileMenu.add("Print");

    newItem.setAccelerator(KeyStroke.getKeyStroke('N', Event.CTRL_MASK));
    openItem.setAccelerator(KeyStroke.getKeyStroke('O', Event.CTRL_MASK));
    saveItem.setAccelerator(KeyStroke.getKeyStroke('S', Event.CTRL_MASK));
    printItem.setAccelerator(KeyStroke.getKeyStroke('P', Event.CTRL_MASK));

    elementMenu.add(lineItem = new JRadioButtonMenuItem("Line", true));
    elementMenu.add(rectangleItem = new JRadioButtonMenuItem("Rectangle", false));
    elementMenu.add(circleItem = new JRadioButtonMenuItem("Circle", false));
    elementMenu.add(curveItem = new JRadioButtonMenuItem("Curve", false));
    ButtonGroup types = new ButtonGroup();
    types.add(lineItem);
    types.add(rectangleItem);
    types.add(circleItem);
    types.add(curveItem);

    lineItem.setAccelerator(KeyStroke.getKeyStroke('L', Event.CTRL_MASK));
    rectangleItem.setAccelerator(KeyStroke.getKeyStroke('E', Event.CTRL_MASK));
    circleItem.setAccelerator(KeyStroke.getKeyStroke('I', Event.CTRL_MASK));
    curveItem.setAccelerator(KeyStroke.getKeyStroke('V', Event.CTRL_MASK));

    elementMenu.addSeparator();

    elementMenu.add(colorMenu); // Add the sub-menu
    colorMenu.add(redItem = new JCheckBoxMenuItem("Red", false));
    colorMenu.add(yellowItem = new JCheckBoxMenuItem("Yellow", false));
    colorMenu.add(greenItem = new JCheckBoxMenuItem("Green", false));
    colorMenu.add(blueItem = new JCheckBoxMenuItem("Blue", true));

    // Add element color accelerators
    redItem.setAccelerator(KeyStroke.getKeyStroke('R', Event.CTRL_MASK));
    yellowItem.setAccelerator(KeyStroke.getKeyStroke('Y', Event.CTRL_MASK));
    greenItem.setAccelerator(KeyStroke.getKeyStroke('G', Event.CTRL_MASK));
    blueItem.setAccelerator(KeyStroke.getKeyStroke('B', Event.CTRL_MASK));

    menuBar.add(fileMenu);
    menuBar.add(elementMenu);
}

From source file:PizzaGridBagLayout.java

public PizzaGridBagLayout() {
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel panel1 = new JPanel();
    panel1.setLayout(new GridBagLayout());
    addItem(panel1, new JLabel("Name:"), 0, 0, 1, 1, GridBagConstraints.EAST);
    addItem(panel1, new JLabel("Phone:"), 0, 1, 1, 1, GridBagConstraints.EAST);
    addItem(panel1, new JLabel("Address:"), 0, 2, 1, 1, GridBagConstraints.EAST);

    addItem(panel1, name, 1, 0, 2, 1, GridBagConstraints.WEST);
    addItem(panel1, phone, 1, 1, 1, 1, GridBagConstraints.WEST);
    addItem(panel1, address, 1, 2, 2, 1, GridBagConstraints.WEST);

    Box sizeBox = Box.createVerticalBox();
    ButtonGroup sizeGroup = new ButtonGroup();
    sizeGroup.add(small);
    sizeGroup.add(medium);/* w  w  w.  j av  a 2  s. c o  m*/
    sizeGroup.add(large);
    sizeBox.add(small);
    sizeBox.add(medium);
    sizeBox.add(large);
    sizeBox.setBorder(BorderFactory.createTitledBorder("Size"));
    addItem(panel1, sizeBox, 0, 3, 1, 1, GridBagConstraints.NORTH);

    Box styleBox = Box.createVerticalBox();

    ButtonGroup styleGroup = new ButtonGroup();
    styleGroup.add(thin);
    styleGroup.add(thick);
    styleBox.add(thin);
    styleBox.add(thick);
    styleBox.setBorder(BorderFactory.

            createTitledBorder("Style"));
    addItem(panel1, styleBox, 1, 3, 1, 1, GridBagConstraints.NORTH);

    Box topBox = Box.createVerticalBox();
    ButtonGroup topGroup = new ButtonGroup();
    topGroup.add(pepperoni);
    topGroup.add(mushrooms);
    topGroup.add(anchovies);
    topBox.add(pepperoni);
    topBox.add(mushrooms);
    topBox.add(anchovies);
    topBox.setBorder(BorderFactory.createTitledBorder("Toppings"));
    addItem(panel1, topBox, 2, 3, 1, 1, GridBagConstraints.NORTH);

    Box buttonBox = Box.createHorizontalBox();
    buttonBox.add(okButton);
    buttonBox.add(Box.createHorizontalStrut(20));
    buttonBox.add(closeButton);
    addItem(panel1, buttonBox, 2, 4, 1, 1, GridBagConstraints.NORTH);

    this.add(panel1);
    this.pack();
    this.setVisible(true);
}

From source file:ToggleButtonCheckBoxRadioButton.java

public TogglePanel() {
    JToggleButton tog = new JToggleButton("Toggle");
    ItemListener listener = new ItemListener() {
        public void itemStateChanged(ItemEvent e) {
            AbstractButton src = (AbstractButton) (e.getSource());
            System.out.println("Toggle: " + src.getText());
        }//  w  w  w  . ja  va 2 s .co  m
    };
    tog.addItemListener(listener);
    add(tog);

    JCheckBox cbox = new JCheckBox("Checkbox");
    cbox.addItemListener(listener);
    add(cbox);
    ButtonGroup btngroup = new ButtonGroup();
    for (int i = 1; i <= 3; i++) {
        JRadioButton radio = new JRadioButton("Radio " + i);
        btngroup.add(radio);
        radio.addItemListener(listener);
        add(radio);
    }
}

From source file:MenuTest.java

public MenuTest() {
    super();//from   ww  w.  j  av  a  2s  .  c  om

    MenuListener listener = new MenuListener() {
        public void menuCanceled(MenuEvent e) {
            dumpInfo("Canceled", e);
        }

        public void menuDeselected(MenuEvent e) {
            dumpInfo("Deselected", e);
        }

        public void menuSelected(MenuEvent e) {
            dumpInfo("Selected", e);
        }

        private void dumpInfo(String s, MenuEvent e) {
            JMenu menu = (JMenu) e.getSource();
            System.out.println(s + ": " + menu.getText());
        }
    };

    JMenu fileMenu = new JMenu("File");
    fileMenu.addMenuListener(listener);
    fileMenu.add(new JMenuItem("Open"));
    fileMenu.add(new JMenuItem("Close"));
    fileMenu.add(new JMenuItem("Exit"));
    JMenu helpMenu = new JMenu("Help");
    helpMenu.addMenuListener(listener);
    helpMenu.add(new JMenuItem("About MenuTest"));
    helpMenu.add(new JMenuItem("Class Hierarchy"));
    helpMenu.addSeparator();
    helpMenu.add(new JCheckBoxMenuItem("Balloon Help"));
    JMenu subMenu = new JMenu("Categories");
    subMenu.addMenuListener(listener);
    JRadioButtonMenuItem rb;
    ButtonGroup group = new ButtonGroup();
    subMenu.add(rb = new JRadioButtonMenuItem("A Little Help", true));
    group.add(rb);
    subMenu.add(rb = new JRadioButtonMenuItem("A Lot of Help"));
    group.add(rb);
    helpMenu.add(subMenu);
    JMenuBar mb = new JMenuBar();
    mb.add(fileMenu);
    mb.add(helpMenu);
    setJMenuBar(mb);
}

From source file:TArea.java

public void init() {
    Container container = getContentPane();

    JPanel panel = new JPanel();
    panel.setLayout(new GridLayout(2, 2));

    resetButton = new JRadioButton("Reset", true);

    addButton = new JRadioButton("Add");
    subtractButton = new JRadioButton("Subtract");
    intersectButton = new JRadioButton("Intersect");
    exclusiveORButton = new JRadioButton("ExclusiveOR");

    ButtonGroup group = new ButtonGroup();
    group.add(resetButton);
    group.add(addButton);//from ww w . j a  v  a  2s.com
    group.add(subtractButton);
    group.add(intersectButton);
    group.add(exclusiveORButton);
    group.add(resetButton);

    resetButton.addActionListener(this);
    addButton.addActionListener(this);
    subtractButton.addActionListener(this);
    intersectButton.addActionListener(this);
    exclusiveORButton.addActionListener(this);

    panel.add(addButton);
    panel.add(subtractButton);
    panel.add(intersectButton);
    panel.add(exclusiveORButton);

    container.add(panel, BorderLayout.NORTH);
    container.add(resetButton, BorderLayout.SOUTH);

    canvas = new DrawingCanvas();
    container.add(canvas);
}

From source file:SimpleBufferedImageDemo.java

public SimpleBufferedImageDemo() {
    super();//from ww  w.  j  ava 2  s.  c o  m
    Container container = getContentPane();

    canvas = new DisplayCanvas();
    container.add(canvas);

    JPanel panel = new JPanel();
    panel.setLayout(new GridLayout(2, 2));
    panel.setBorder(new TitledBorder("Select an Option and Display Image..."));

    buffButton = new JRadioButton("Buffered");
    buffButton.addActionListener(new ButtonListener());
    nonBuffButton = new JRadioButton("Non-Buffered", true);
    nonBuffButton.addActionListener(new ButtonListener());
    ButtonGroup group = new ButtonGroup();
    group.add(buffButton);
    group.add(nonBuffButton);

    displayButton = new JButton("Display");
    displayButton.addActionListener(new ButtonListener());
    clearButton = new JButton("Clear");
    clearButton.addActionListener(new ButtonListener());

    panel.add(nonBuffButton);
    panel.add(buffButton);
    panel.add(displayButton);
    panel.add(clearButton);

    container.add(BorderLayout.SOUTH, panel);

    addWindowListener(new WindowEventHandler());
    pack();
    setVisible(true);
}

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 w w  .jav a2 s .  c  om
    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:SimplelookandfeelExample.java

public SimplelookandfeelExample() {
    // Create the buttons.
    JButton button = new JButton("Hello, world");
    button.setMnemonic('h'); //for looks only; button does nada

    metalButton = new JRadioButton(metal);
    metalButton.setMnemonic('o');
    metalButton.setActionCommand(metalClassName);

    motifButton = new JRadioButton(motif);
    motifButton.setMnemonic('m');
    motifButton.setActionCommand(motifClassName);

    windowsButton = new JRadioButton(windows);
    windowsButton.setMnemonic('w');
    windowsButton.setActionCommand(windowsClassName);

    // Group the radio buttons.
    ButtonGroup group = new ButtonGroup();
    group.add(metalButton);
    group.add(motifButton);//from   w w  w  .  j  av  a2s  . c om
    group.add(windowsButton);

    // Register a listener for the radio buttons.
    RadioListener myListener = new RadioListener();
    metalButton.addActionListener(myListener);
    motifButton.addActionListener(myListener);
    windowsButton.addActionListener(myListener);

    add(button);
    add(metalButton);
    add(motifButton);
    add(windowsButton);
}