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

StringgetSelectedButtonText(ButtonGroup buttonGroup)
get Selected Button Text
for (Enumeration<AbstractButton> buttons = buttonGroup.getElements(); buttons.hasMoreElements();) {
    AbstractButton button = buttons.nextElement();
    if (button.isSelected()) {
        return button.getText();
return null;
StringgetSelectedButtonText(ButtonGroup buttonGroup)
Determines the label of the current selected button.
for (Enumeration<AbstractButton> buttons = buttonGroup.getElements(); buttons.hasMoreElements();) {
    AbstractButton button = buttons.nextElement();
    if (button.isSelected()) {
        return button.getText();
return null;
JToggleButtongetSoftToggleButton(String string)
get Soft Toggle Button
JToggleButton b = new JToggleButton(string);
b.putClientProperty("JButton.buttonType", "segmentedRoundRect");
b.putClientProperty("JButton.segmentPosition", "only");
return b;
intgetTextXOffset(AbstractButton c)
get Text X Offset
int xoffset = c.getPreferredSize().width;
Border b = c.getBorder();
if (b != null) {
    xoffset -= b.getBorderInsets(c).right;
String text = c.getText();
if (text != null) {
    xoffset -= c.getFontMetrics(c.getFont()).stringWidth(text);
...
JToggleButtongetTogglableIconButton(ImageIcon icon)
get Togglable Icon Button
final ImageIcon selectedIcon = icon;
final ImageIcon unselectedIcon = new ImageIcon(GrayFilter.createDisabledImage(icon.getImage()));
final JToggleButton button = new JToggleButton(icon);
button.setFocusable(false);
button.setContentAreaFilled(false);
button.setBorder(null);
final Runnable setSelected = new Runnable() {
    @Override
...
JButtongetWindowFileChooserOKButton(JFileChooser chooser)
get Window File Chooser OK Button
return (JButton) (((Container) ((Container) ((Container) chooser.getComponent(2)).getComponent(2))
        .getComponent(4)).getComponent(1));
ButtonGroupgroupButtons(AbstractButton... buttons)
group Buttons
ButtonGroup group = new ButtonGroup();
for (AbstractButton button : buttons) {
    group.add(button);
return group;
ButtonGroupgroupButtons(final Container container, final boolean recursive)
Groups all buttons inside this container and all subcontainers if requested and returns created button group.
final ButtonGroup buttonGroup = new ButtonGroup();
groupButtons(container, recursive, buttonGroup);
return buttonGroup;
voidgrowFont(JComponent button)
grow Font
growFont(button, 1);
booleanhasIcon(AbstractButton button)
Checks whether the specified button has associated icon.
return (button.getIcon() != null);