Java Utililty Methods JButton

List of utility methods to do JButton

Description

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

Method

voidreduceNimbusButtonMargin(final JButton button)
Reduce the margins around the content of a button for the Nimbus look and feel
UIDefaults buttonDefaults = new UIDefaults();
buttonDefaults.put("Button.contentMargins", new Insets(6, 6, 6, 6));
button.putClientProperty("Nimbus.Overrides", buttonDefaults);
button.putClientProperty("Nimbus.Overrides.InheritDefaults", Boolean.FALSE);
voidregisterCancelButton(final JButton cancelButton)
Register the escape key so that it can be used to cancel the associated JDialog.
KeyStroke stroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
JDialog dialog = (JDialog) SwingUtilities.getWindowAncestor(cancelButton);
dialog.getRootPane().registerKeyboardAction(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent ae) {
        cancelButton.doClick();
}, stroke, JComponent.WHEN_IN_FOCUSED_WINDOW);
...
voidremoveAllActionListeners(JButton button)
remove All Action Listeners
ActionListener[] listeners = button.getActionListeners();
for (int i = 0; i < listeners.length; i++) {
    button.removeActionListener(listeners[i]);
voidremoveAllListeners(JButton bouton)
remove All Listeners
for (ActionListener al : bouton.getActionListeners()) {
    bouton.removeActionListener(al);
voidremoveMargins(JButton button)
Removes all extra spacing around the button so other components are layed out right next to it.
button.setBorder(null);
button.setBorderPainted(false);
button.setMargin(new Insets(0, 0, 0, 0));
voidrenderImageOnly(JButton button)
Changes style formatting of a button so that only the images will show; no additional drawing.
button.setBorderPainted(false);
button.setContentAreaFilled(false);
button.setFocusPainted(false);
button.setOpaque(false);
voidsetButtonNo(JButton anButton)
setButtonOk
anButton.setText(BUTTON_NO);
voidsetCancelButton(final JRootPane rp, final JButton b)
set Cancel Button
rp.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
        "cancel");
rp.getActionMap().put("cancel", new AbstractAction() {
    private static final long serialVersionUID = 1L;
    public void actionPerformed(ActionEvent ev) {
        b.doClick();
});
...
voidsetEmptyBorder(JButton button, int paddingTop, int paddingLeft, int paddingBottom, int paddingRight)
set Empty Border
if (button != null) {
    Border emptyBorder = BorderFactory.createEmptyBorder(paddingTop, paddingLeft, paddingBottom,
            paddingRight);
    button.setBorder(emptyBorder);
voidsetEnable(boolean b, JButton... bs)
set Enable
for (JButton button : bs) {
    button.setEnabled(b);