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

voidsetScaledIcon(final AbstractButton button, final ImageIcon icon, final int orientation, final double scale)
Sets the button's default icon, scales it to a given factor of the button's preferred size and aligns it to the given orientation.
final Dimension preferredSize = button.getPreferredSize();
final Insets margin = button.getMargin();
final int minimumSize = Math.min(preferredSize.width - margin.left - margin.right,
        preferredSize.height - margin.top - margin.bottom);
int size = minimumSize;
final double actualScale = Math.abs(scale);
if (actualScale != 1) {
    final double scaledSize = actualScale * size;
...
voidsetSelected(final AbstractButton abstractButton, final boolean isSelected)
set Selected
doSetSelected(abstractButton, isSelected);
voidsetSelectedButton(ButtonGroup group, int index)
set Selected Button
Enumeration<AbstractButton> enumeration = group.getElements();
int i = 0;
while (enumeration.hasMoreElements()) {
    AbstractButton button = enumeration.nextElement();
    group.setSelected(button.getModel(), index == i);
    i++;
voidsetSelectedSilently(AbstractButton x, boolean b)
set Selected Silently
ActionListener[] al = x.getActionListeners();
if (al != null && al.length > 0) {
    for (ActionListener a : al)
        x.removeActionListener(a);
ItemListener[] il = x.getItemListeners();
if (il != null && il.length > 0) {
    for (ItemListener a : il)
...
voidsetToggleGroups(final JToggleButton... buttons)
Bind the toggle buttons into a group in which only one can be selected.
for (int i = 0; i < buttons.length; i++) {
    final int t = i;
    buttons[i].addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            boolean flag = buttons[t].isSelected();
            if (flag) {
                for (int i = 0; i < buttons.length; i++)
...
voidsetToggling(AbstractButton b)
Set the specified button such that it alternates between being selected and not whenever it is pushed.
if (_toggler == null) {
    _toggler = new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            AbstractButton but = (AbstractButton) event.getSource();
            but.setSelected(!but.isSelected());
    };
b.addActionListener(_toggler);
voidsetWithoutNotifyingListeners(AbstractButton button, boolean selected)
set Without Notifying Listeners
if (button == null)
    return;
Action a = button.getAction();
String text = button.getText();
String tooltip = button.getToolTipText();
ItemListener[] il = button.getItemListeners();
if (il != null) {
    for (ItemListener x : il)
...
intshowJFileChooser(javax.swing.JFileChooser chooser, java.awt.Component parent, java.lang.String approveButtonText)
Utility method for avoiding of memory leak in JDK 1.3 / JFileChooser.showDialog(...)
if (approveButtonText != null) {
    chooser.setApproveButtonText(approveButtonText);
    chooser.setDialogType(javax.swing.JFileChooser.CUSTOM_DIALOG);
Frame frame = null;
Dialog parentDlg = null;
if (parent instanceof Dialog)
    parentDlg = (Dialog) parent;
...
intshowJFileChooser(JFileChooser chooser, Component parent, String approveButtonText)
Displays the given file chooser.
if (approveButtonText != null) {
    chooser.setApproveButtonText(approveButtonText);
    chooser.setDialogType(javax.swing.JFileChooser.CUSTOM_DIALOG);
Frame frame = (parent instanceof Frame) ? (Frame) parent
        : (Frame) javax.swing.SwingUtilities.getAncestorOfClass(java.awt.Frame.class, parent);
String title = chooser.getDialogTitle();
if (title == null) {
...
intshowOptions(int width, int type, String title, String text, Object... buttons)
Shows a message with options presented as an array of buttons.
return JOptionPane.showOptionDialog(null,
        String.format("<html><body style=\"width:%dpx\">%s</body></html>", normalizeMessageWidth(width),
                text),
        title, JOptionPane.DEFAULT_OPTION, normalizeIconType(type), null, buttons, buttons[0]) + 1;