Java JButton Settings setToggleGroups(final JToggleButton... buttons)

Here you can find the source of setToggleGroups(final JToggleButton... buttons)

Description

Bind the toggle buttons into a group in which only one can be selected.

License

Open Source License

Parameter

Parameter Description
buttons the toggle buttons to bind

Declaration

public static void setToggleGroups(final JToggleButton... buttons) 

Method Source Code

//package com.java2s;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JToggleButton;

public class Main {
    /**//from w  w  w.j  a v  a  2s .com
     * Bind the toggle buttons into a group in which only one can be selected.
     * 
     * @param buttons
     *            the toggle buttons to bind
     */
    public static void setToggleGroups(final JToggleButton... buttons) {
        for (int i = 0; i < buttons.length; i++) {
            final int t = i;
            buttons[i].addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    boolean flag = buttons[t].isSelected();
                    if (flag) {
                        for (int i = 0; i < buttons.length; i++)
                            if (i != t)
                                buttons[i].setSelected(false);
                    } else
                        buttons[t].setSelected(true);
                }
            });
        }
    }
}

Related

  1. setRadioButtonSelected(Window window, String buttonText)
  2. setScaledIcon(final AbstractButton button, final ImageIcon icon, final int orientation, final double scale)
  3. setSelected(final AbstractButton abstractButton, final boolean isSelected)
  4. setSelectedButton(ButtonGroup group, int index)
  5. setSelectedSilently(AbstractButton x, boolean b)
  6. setToggling(AbstractButton b)
  7. setWithoutNotifyingListeners(AbstractButton button, boolean selected)
  8. showJFileChooser(javax.swing.JFileChooser chooser, java.awt.Component parent, java.lang.String approveButtonText)
  9. showJFileChooser(JFileChooser chooser, Component parent, String approveButtonText)