Java Utililty Methods JButton Settings

List of utility methods to do JButton Settings

Description

The list of methods to do JButton Settings are organized into topic(s).

Method

voidsetButtonInsetsRecursive(Insets insets, Container container)
set Button Insets Recursive
Component[] components = container.getComponents();
for (int i = 0; i < components.length; i++) {
    Component c = components[i];
    if (c instanceof JButton || c instanceof JToggleButton) {
        if (!(c instanceof JCheckBox) && !(c instanceof JRadioButton))
            ((AbstractButton) c).setMargin(insets);
    } else if (c instanceof Container)
        setButtonInsetsRecursive(insets, (Container) c);
...
voidsetButtonSelected(ButtonGroup buttonGroup, int selected)
set Button Selected
ArrayList<AbstractButton> buttons = Collections.list(buttonGroup.getElements());
if (buttons.isEmpty())
    return; 
if (selected < 0 || selected >= buttons.size())
    selected = 0; 
AbstractButton buttonSelected = buttons.get(selected);
buttonSelected.setSelected(true);
ActionEvent actionEvent = new ActionEvent(buttonSelected, ActionEvent.ACTION_PERFORMED,
...
voidsetButtonStyle(AbstractButton btn)
set Button Style
getBasicFont();
btn.setFont(BASIC_FONT);
btn.setForeground(BASIC_BUTTON_COLOR);
btn.setBackground(BACKGROUND);
voidsetButtonText(ButtonGroup buttonGroup, List texts)
setter method for button text.
int index = 0;
for (Enumeration<AbstractButton> buttons = buttonGroup.getElements(); buttons.hasMoreElements();) {
    AbstractButton button = buttons.nextElement();
    button.setText(texts.get(index).toString());
    index++;
voidsetHelpIDString(javax.swing.AbstractButton btn, String id)
set Help ID String
btn.putClientProperty("HelpID", id);
voidsetHorizontalMargin(AbstractButton button, int hMargin)
Sets the left and right margin of an AbstractButton in a L&F-independent manner (some L&Fs set this value in the margin, while others ignore this property and use the border instead).
Insets margin = button.getMargin();
margin.left = margin.right = hMargin;
Border empty = BorderFactory.createEmptyBorder(margin.top, margin.left, margin.bottom, margin.right);
Border outer = button.getBorder();
Border compound = BorderFactory.createCompoundBorder(outer, empty);
button.setMargin(new Insets(0, 0, 0, 0));
button.setBorder(compound);
voidsetImageIcon(AbstractButton abstractButton, URL url)
set Image Icon
if (url != null) {
    abstractButton.setIcon(new ImageIcon(url));
} else {
    abstractButton.setIcon(null);
voidsetMnemonic(AbstractButton actionComponent)
Translate the character following MNEMONIC_CHARACTER in the label by a mnemonic
String componentLabel = actionComponent.getText();
int charPosition = getMnemonicCharPos(componentLabel);
if (charPosition >= 0) {
    actionComponent.setMnemonic(Character.toUpperCase(componentLabel.charAt(charPosition + 1)));
    actionComponent.setText(clearMnemonic(charPosition, componentLabel));
voidsetPreferredWidth(JComponent button, int minWidth)
Sets the minimum width of a JButton .
Dimension size = button.getPreferredSize();
if (size.width < minWidth) {
    size.width = minWidth;
    button.setPreferredSize(size);
booleansetRadioButtonSelected(Window window, String buttonText)
set Radio Button Selected
final JRadioButton rb = findRadioButton(window, buttonText);
if (rb == null)
    return false;
if (rb.isSelected())
    return true;
rb.doClick();
return true;