Example usage for javax.swing JRadioButton JRadioButton

List of usage examples for javax.swing JRadioButton JRadioButton

Introduction

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

Prototype

public JRadioButton(String text) 

Source Link

Document

Creates an unselected radio button with the specified text.

Usage

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  .j a  v  a 2  s  .  co m
    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:OptionDialogTest.java

/**
 * Constructs a button panel.//w w w  . j  a v a  2  s  .com
 * @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]);
    }
}

From source file:BorderTest.java

public void addRadioButton(String buttonName, final Border b) {
    JRadioButton button = new JRadioButton(buttonName);
    button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            demoPanel.setBorder(b);//from w w  w .  j av  a2s .  co  m
        }
    });
    group.add(button);
    buttonPanel.add(button);
}

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);/* w w  w. j a  v  a 2 s . c om*/
        }
        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:GroupRadio.java

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

From source file:RadioButtonDemo.java

public RadioButtonDemo() {
    super(new BorderLayout());

    //Create the radio buttons.
    JRadioButton birdButton = new JRadioButton(birdString);
    birdButton.setMnemonic(KeyEvent.VK_B);
    birdButton.setActionCommand(birdString);
    birdButton.setSelected(true);/*  ww  w.ja  v  a  2 s.com*/

    JRadioButton catButton = new JRadioButton(catString);
    catButton.setMnemonic(KeyEvent.VK_C);
    catButton.setActionCommand(catString);

    JRadioButton dogButton = new JRadioButton(dogString);
    dogButton.setMnemonic(KeyEvent.VK_D);
    dogButton.setActionCommand(dogString);

    JRadioButton rabbitButton = new JRadioButton(rabbitString);
    rabbitButton.setMnemonic(KeyEvent.VK_R);
    rabbitButton.setActionCommand(rabbitString);

    JRadioButton pigButton = new JRadioButton(pigString);
    pigButton.setMnemonic(KeyEvent.VK_P);
    pigButton.setActionCommand(pigString);

    //Group the radio buttons.
    ButtonGroup group = new ButtonGroup();
    group.add(birdButton);
    group.add(catButton);
    group.add(dogButton);
    group.add(rabbitButton);
    group.add(pigButton);

    //Register a listener for the radio buttons.
    birdButton.addActionListener(this);
    catButton.addActionListener(this);
    dogButton.addActionListener(this);
    rabbitButton.addActionListener(this);
    pigButton.addActionListener(this);

    //Set up the picture label.
    picture = new JLabel(createImageIcon("images/" + birdString + ".gif"));

    //The preferred size is hard-coded to be the width of the
    //widest image and the height of the tallest image.
    //A real program would compute this.
    picture.setPreferredSize(new Dimension(177, 122));

    //Put the radio buttons in a column in a panel.
    JPanel radioPanel = new JPanel(new GridLayout(0, 1));
    radioPanel.add(birdButton);
    radioPanel.add(catButton);
    radioPanel.add(dogButton);
    radioPanel.add(rabbitButton);
    radioPanel.add(pigButton);

    add(radioPanel, BorderLayout.LINE_START);
    add(picture, BorderLayout.CENTER);
    setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
}

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);//  w  ww.j a v a2s  .c  o m
    group.add(motifButton);
    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);
}

From source file:net.sourceforge.doddle_owl.ui.GeneralOntologySelectionPanel.java

public GeneralOntologySelectionPanel(NameSpaceTable nsTable) {
    nameSpaceTable = nsTable;/*from www.  j a v  a 2s  .c  o  m*/
    edrCheckBox = new JCheckBox(Translator.getTerm("GenericEDRCheckBox"), false);
    edrCheckBox.addActionListener(this);
    edrtCheckBox = new JCheckBox(Translator.getTerm("TechnicalEDRCheckBox"), false);
    edrtCheckBox.addActionListener(this);
    wnCheckBox = new JCheckBox(Translator.getTerm("WordNetCheckBox"), false);
    wnCheckBox.addActionListener(this);
    wnVersionSelectionPanel = new JPanel();
    wn30RadioButton = new JRadioButton("3.0");
    wn30RadioButton.addChangeListener(this);
    wn31RadioButton = new JRadioButton("3.1");
    wn31RadioButton.setSelected(true);
    wn31RadioButton.addChangeListener(this);
    ButtonGroup group = new ButtonGroup();
    group.add(wn30RadioButton);
    group.add(wn31RadioButton);
    wnVersionSelectionPanel.add(wnCheckBox);
    wnVersionSelectionPanel.add(wn30RadioButton);
    wnVersionSelectionPanel.add(wn31RadioButton);
    JPanel borderPanel = new JPanel();
    borderPanel.setLayout(new BorderLayout());
    borderPanel.add(wnVersionSelectionPanel, BorderLayout.WEST);

    jpnWnCheckBox = new JCheckBox(Translator.getTerm("JpnWordNetCheckBox"), false);
    jpnWnCheckBox.addActionListener(this);
    jwoCheckBox = new JCheckBox(Translator.getTerm("JWOCheckBox"), false);
    jwoCheckBox.addActionListener(this);
    JPanel checkPanel = new JPanel();
    checkPanel.add(borderPanel);
    checkPanel.add(jpnWnCheckBox);
    checkPanel.add(jwoCheckBox);
    checkPanel.add(edrCheckBox);
    checkPanel.add(edrtCheckBox);
    setLayout(new BorderLayout());
    add(checkPanel, BorderLayout.WEST);

    generalOntologyDirLabel = new JLabel(Utils.TEMP_DIR);
    removeGeneralOntologyDirButton = new JButton(Translator.getTerm("RemoveGeneralOntologyDirectoryButton"));
    removeGeneralOntologyDirButton.addActionListener(this);
    JPanel buttonPanel = new JPanel();
    buttonPanel.setLayout(new BorderLayout());
    buttonPanel.add(generalOntologyDirLabel, BorderLayout.CENTER);
    buttonPanel.add(removeGeneralOntologyDirButton, BorderLayout.WEST);
    add(buttonPanel, BorderLayout.SOUTH);
}

From source file:eu.apenet.dpt.standalone.gui.XsdAdderActionListener.java

public void actionPerformed(ActionEvent e) {
    JFileChooser xsdChooser = new JFileChooser();
    xsdChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);

    if (xsdChooser.showOpenDialog(parent) == JFileChooser.APPROVE_OPTION) {
        File file = xsdChooser.getSelectedFile();
        if (isXSD(file)) {
            XsdInfoQueryComponent xsdInfoQueryComponent = new XsdInfoQueryComponent(labels, file.getName());

            int result = JOptionPane.showConfirmDialog(parent, xsdInfoQueryComponent, "title",
                    JOptionPane.OK_CANCEL_OPTION);
            if (result == JOptionPane.OK_OPTION) {
                if (StringUtils.isEmpty(xsdInfoQueryComponent.getName())) {
                    errorMessage();/* www  .  j av  a 2s.  c om*/
                } else {
                    if (saveXsd(file, xsdInfoQueryComponent.getName(), false,
                            xsdInfoQueryComponent.getXsdVersion(), xsdInfoQueryComponent.getFileType())) {
                        JRadioButton newButton = new JRadioButton(xsdInfoQueryComponent.getName());
                        newButton.addActionListener(new XsdSelectorListener(dataPreparationToolGUI));
                        dataPreparationToolGUI.getGroupXsd().add(newButton);
                        dataPreparationToolGUI.getAPEPanel().getApeTabbedPane().addToXsdPanel(newButton);
                        dataPreparationToolGUI.getAPEPanel().getApeTabbedPane()
                                .addToXsdPanel(Box.createRigidArea(new Dimension(0, 10)));
                        JOptionPane.showMessageDialog(parent, labels.getString("xsdSaved") + ".",
                                labels.getString("fileSaved"), JOptionPane.INFORMATION_MESSAGE, Utilities.icon);
                    } else {
                        errorMessage();
                    }
                }
            }
        } else {
            errorMessage();
        }
    }
}

From source file:LNFSwitcher.java

/** Construct a program... */
public LNFSwitcher() {
    super();// w w w.j a  v a 2 s  . com
    theFrame = new JFrame("LNF Switcher");
    theFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    cp = theFrame.getContentPane();
    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();
    // System.out.println(defaultLookAndFeel);
    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);
}