Java JButton Settings setButtonSelected(ButtonGroup buttonGroup, int selected)

Here you can find the source of setButtonSelected(ButtonGroup buttonGroup, int selected)

Description

set Button Selected

License

Open Source License

Declaration

public static void setButtonSelected(ButtonGroup buttonGroup, int selected) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

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

import java.util.ArrayList;
import java.util.Collections;

import javax.swing.AbstractButton;
import javax.swing.ButtonGroup;

public class Main {
    public static void setButtonSelected(ButtonGroup buttonGroup, int selected) {
        ArrayList<AbstractButton> buttons = Collections.list(buttonGroup.getElements());
        if (buttons.isEmpty())
            return; // Safety: The moron forgot to put radio buttons into this group!  Don't select any!
        if (selected < 0 || selected >= buttons.size())
            selected = 0; // Default is zero if you pass an out of range value.
        AbstractButton buttonSelected = buttons.get(selected);
        buttonSelected.setSelected(true);
        //  Be consistent, when set, do this also:
        ActionEvent actionEvent = new ActionEvent(buttonSelected, ActionEvent.ACTION_PERFORMED,
                buttonSelected.getActionCommand());
        for (ActionListener actionListener : buttonSelected.getActionListeners()) {
            actionListener.actionPerformed(actionEvent);
        }/*from   w  ww  . j a va 2 s  .c o  m*/
    }
}

Related

  1. setAutoMnemonic(AbstractButton button)
  2. setBtnValue(ButtonGroup group, String value)
  3. setButton(AbstractButton button, Icon icon, ActionListener buttonListener)
  4. setButtonContentMargin(AbstractButton button, Insets margin)
  5. setButtonInsetsRecursive(Insets insets, Container container)
  6. setButtonStyle(AbstractButton btn)
  7. setButtonText(ButtonGroup buttonGroup, List texts)
  8. setHelpIDString(javax.swing.AbstractButton btn, String id)
  9. setHorizontalMargin(AbstractButton button, int hMargin)