Java tutorial
//package com.java2s; /* * This file is part of WebLookAndFeel library. * * WebLookAndFeel library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * WebLookAndFeel library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with WebLookAndFeel library. If not, see <http://www.gnu.org/licenses/>. */ import java.awt.Component; import java.awt.Container; import javax.swing.AbstractButton; import javax.swing.ButtonGroup; public class Main { /** * Groups all buttons inside this container and returns created button * group. * * @param container * container to process * @return created button group */ public static ButtonGroup groupButtons(final Container container) { return groupButtons(container, false); } /** * Groups all buttons inside this container and all subcontainers if * requested and returns created button group. * * @param container * container to process * @param recursive * whether to check all subcontainers or not * @return created button group */ public static ButtonGroup groupButtons(final Container container, final boolean recursive) { final ButtonGroup buttonGroup = new ButtonGroup(); groupButtons(container, recursive, buttonGroup); return buttonGroup; } /** * Groups all buttons inside this container and all subcontainers if * requested and returns created button group. * * @param container * container to process * @param recursive * whether to check all subcontainers or not * @param buttonGroup * button group */ public static void groupButtons(final Container container, final boolean recursive, final ButtonGroup buttonGroup) { for (final Component component : container.getComponents()) { if (component instanceof AbstractButton) { buttonGroup.add((AbstractButton) component); } if (recursive) { if (component instanceof Container) { groupButtons(container, true); } } } } /** * Groups specified buttons and returns created button group. * * @param buttons * buttons to group * @return created button group */ public static ButtonGroup groupButtons(final AbstractButton... buttons) { final ButtonGroup buttonGroup = new ButtonGroup(); groupButtons(buttonGroup, buttons); return buttonGroup; } /** * Groups buttons in the specified button group. * * @param buttonGroup * button group * @param buttons * buttons to group */ public static void groupButtons(final ButtonGroup buttonGroup, final AbstractButton... buttons) { for (final AbstractButton button : buttons) { buttonGroup.add(button); } } }