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

voidScaleButtonIcon(JToggleButton btn, int width, int height, int fontsize)
Scale Button Icon
btn.setMargin(null);
int iconWidth = width;
int iconHeigth = height;
if (btn.getText() != null && !btn.getText().isEmpty()) {
    Font font = btn.getFont();
    btn.setFont(new Font(font.getName(), font.getStyle(), fontsize));
    FontMetrics fm = btn.getFontMetrics(font);
    btn.setHorizontalTextPosition(JButton.CENTER);
...
StringSelectedOptionButton(Container container)
Selected Option Button
for (Component optIterator : container.getComponents()) {
    if (optIterator instanceof JRadioButton) {
        JRadioButton optSelected = (JRadioButton) optIterator;
        if (optSelected.isSelected()) {
            return optSelected.getActionCommand();
return "";
FileselectFile(final int openMode, final String title, final String buttonText, final String lastDirectoryUsed, final Component parent, final String suffix, final String description)
Function to open a JFileChooser and set it properly.
JFileChooser chooser = new JFileChooser(lastDirectoryUsed);
chooser.setFileHidingEnabled(false);
chooser.setDialogTitle(title);
chooser.setFileFilter(new javax.swing.filechooser.FileFilter() {
    @Override
    public boolean accept(File pathname) {
        if (pathname.isDirectory()) {
            return true;
...
voidselectWithoutNotifyingListeners(AbstractButton ab, boolean selected)
select Without Notifying Listeners
ItemListener[] il = ab.getItemListeners();
if (il != null) {
    for (ItemListener i : il)
        ab.removeItemListener(i);
ActionListener[] al = ab.getActionListeners();
if (al != null) {
    for (ActionListener i : al)
...
voidsetAction(AbstractButton btn, ActionListener listener, String actionCommand)
set Action
btn.addActionListener(listener);
btn.setActionCommand(actionCommand);
voidsetAllEnabled(final ButtonGroup buttonGroup, final boolean enabled)
Enables (or disables) all the buttons in a button group.
for (final AbstractButton button : Collections.list(buttonGroup.getElements())) {
    button.setEnabled(enabled);
voidsetAutoMnemonic(AbstractButton button)
Sets the mnemonic for the given button using jEdit convention, taking the letter after the dollar.
String label = button.getText();
char mnemonic;
int index = label.indexOf('$');
if (index != -1 && label.length() - index > 1) {
    mnemonic = Character.toLowerCase(label.charAt(index + 1));
    label = label.substring(0, index).concat(label.substring(++index));
} else {
    mnemonic = '\0';
...
voidsetBtnValue(ButtonGroup group, String value)
set Btn Value
for (Enumeration<AbstractButton> e = group.getElements(); e.hasMoreElements();) {
    AbstractButton btn = e.nextElement();
    if (btn.getActionCommand().equals(value)) {
        group.setSelected(btn.getModel(), true);
        break;
voidsetButton(AbstractButton button, Icon icon, ActionListener buttonListener)

Set Button using given arguments.

button.setIcon(icon);
button.addActionListener(buttonListener);
button.setContentAreaFilled(false);
voidsetButtonContentMargin(AbstractButton button, Insets margin)
Sets the content margin of a button (for Nimbus L&F).
UIDefaults defaults = new UIDefaults();
defaults.put("Button.contentMargins", margin);
defaults.put("ToggleButton.contentMargins", margin);
button.putClientProperty("Nimbus.Overrides", defaults);