Java tutorial
//package com.java2s; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JToggleButton; public class Main { /** * 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); } }); } } }