Here you can find the source of setToggleGroups(final JToggleButton... buttons)
Parameter | Description |
---|---|
buttons | the toggle buttons to bind |
public static void setToggleGroups(final JToggleButton... buttons)
//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); } }); } } }