Java JButton Create createRadioButtonGrouping(String elements[], String title)

Here you can find the source of createRadioButtonGrouping(String elements[], String title)

Description

create Radio Button Grouping

License

Open Source License

Declaration

public static Container createRadioButtonGrouping(String elements[],
            String title) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;

public class Main {
    public static Container createRadioButtonGrouping(String elements[]) {
        return createRadioButtonGrouping(elements, null, null, null, null);
    }/*from   w  w  w. j  a v  a  2  s .c om*/

    public static Container createRadioButtonGrouping(String elements[],
            String title) {
        return createRadioButtonGrouping(elements, title, null, null, null);
    }

    public static Container createRadioButtonGrouping(String elements[],
            String title, ItemListener itemListener) {
        return createRadioButtonGrouping(elements, title, null,
                itemListener, null);
    }

    public static Container createRadioButtonGrouping(String elements[],
            String title, ActionListener actionListener) {
        return createRadioButtonGrouping(elements, title, actionListener,
                null, null);
    }

    public static Container createRadioButtonGrouping(String elements[],
            String title, ActionListener actionListener,
            ItemListener itemListener) {
        return createRadioButtonGrouping(elements, title, actionListener,
                itemListener, null);
    }

    public static Container createRadioButtonGrouping(String elements[],
            String title, ActionListener actionListener,
            ItemListener itemListener, ChangeListener changeListener) {
        JPanel panel = new JPanel(new GridLayout(0, 1));
        // If title set, create titled border
        if (title != null) {
            Border border = BorderFactory.createTitledBorder(title);
            panel.setBorder(border);
        }
        // Create group
        ButtonGroup group = new ButtonGroup();
        JRadioButton aRadioButton;
        // For each String passed in:
        // Create button, add to panel, and add to group
        for (int i = 0, n = elements.length; i < n; i++) {
            aRadioButton = new JRadioButton(elements[i]);
            panel.add(aRadioButton);
            group.add(aRadioButton);
            if (actionListener != null) {
                aRadioButton.addActionListener(actionListener);
            }
            if (itemListener != null) {
                aRadioButton.addItemListener(itemListener);
            }
            if (changeListener != null) {
                aRadioButton.addChangeListener(changeListener);
            }
        }
        return panel;
    }
}

Related

  1. CreateOptionButtonGroup(Box boxGeneric, Border bdrButtonGroup, String[] elements, String[] commandActions, Dimension elementDimension)
  2. createRadioButton(JComponent parent, String text)
  3. createRadioButton(String name, String command, boolean isSelected, ActionListener listener)
  4. createRadioButton(String text, ButtonGroup buttonGroup, ActionListener... listeners)
  5. createRadioButton(String text, int mnemonic, ActionListener listener, boolean selected)
  6. createScreenCaptureButton()
  7. createSimpleTextButton(String caption, Action action)
  8. createSmallButton()
  9. createSmallButton(Icon icon, String tooltip, ActionListener listener)