Java JButton Settings setRadioButtonSelected(Window window, String buttonText)

Here you can find the source of setRadioButtonSelected(Window window, String buttonText)

Description

set Radio Button Selected

License

Open Source License

Declaration

static boolean setRadioButtonSelected(Window window, String buttonText) 

Method Source Code


//package com.java2s;
// it under the terms of the GNU General Public License as published by

import java.awt.Component;
import java.awt.Container;
import java.awt.Window;

import javax.swing.JRadioButton;

public class Main {
    static boolean setRadioButtonSelected(Window window, String buttonText) {
        final JRadioButton rb = findRadioButton(window, buttonText);
        if (rb == null)
            return false;

        if (rb.isSelected())
            return true;

        rb.doClick();/*  w  w w . j a v a  2 s  .  c om*/
        return true;
    }

    /**
     * Traverse a container hierarchy and returns the button with
     * the given text
     *
     */
    static JRadioButton findRadioButton(Container container, String text) {
        Component[] components = container.getComponents();

        for (Component component : components) {
            if (component instanceof JRadioButton) {
                JRadioButton button = (JRadioButton) component;
                if (button.getText().equals(text)) {
                    return button;
                }
            } else if (component instanceof Container) {
                JRadioButton button = findRadioButton((Container) component, text);
                if (button != null) {
                    return button;
                }
            }
        }

        return null;
    }
}

Related

  1. setHelpIDString(javax.swing.AbstractButton btn, String id)
  2. setHorizontalMargin(AbstractButton button, int hMargin)
  3. setImageIcon(AbstractButton abstractButton, URL url)
  4. setMnemonic(AbstractButton actionComponent)
  5. setPreferredWidth(JComponent button, int minWidth)
  6. setScaledIcon(final AbstractButton button, final ImageIcon icon, final int orientation, final double scale)
  7. setSelected(final AbstractButton abstractButton, final boolean isSelected)
  8. setSelectedButton(ButtonGroup group, int index)
  9. setSelectedSilently(AbstractButton x, boolean b)