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

voidcopyButtonWidth(JButton toButton, JButton fromButton)
Makes a button as wide as another given button.
toButton.setPreferredSize(fromButton.getPreferredSize());
toButton.setMaximumSize(fromButton.getMaximumSize());
voiddoButtonClick(JButton button)
Performs visually visible click on the button component.
button.doClick(BUTTON_CLICK_TIME);
voiddrawCharacter(JPanel contentPane, ActionListener listener, String url, int x, int y, List buttons)
draw Character
BufferedImage originalImage;
try {
    try {
        originalImage = ImageIO.read(new URL(url));
    } catch (Exception e) {
        originalImage = ImageIO.read(new URL("http://s3.amazonaws.com/MinecraftSkins/char.png"));
    int type = BufferedImage.TYPE_INT_ARGB;
...
voidenableEnter(JButton b)
Enable activating button on Enter (which is replaced with spacebar for certain Look-And-Feels)
b.setFocusable(true);
b.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "enter");
b.getActionMap().put("enter", b.getAction());
voidensureButtonWidth(JButton button, int width)
Ensures a button has a specific minimum width.
Dimension prefSize = button.getPreferredSize();
if (prefSize.width < width) {
    prefSize.width = width;
    button.setPreferredSize(prefSize);
voidenterPressesWhenFocused(final JButton button)
Registers the enter key a JButton .
button.registerKeyboardAction(
        button.getActionForKeyStroke(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0, false)),
        KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, false), JComponent.WHEN_FOCUSED);
button.registerKeyboardAction(
        button.getActionForKeyStroke(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0, true)),
        KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, true), JComponent.WHEN_FOCUSED);
voidequalizeButtons(JButton... buttons)
equalize Buttons
int maxWidth = 0;
int maxHeight = 0;
for (JButton button : buttons) {
    Dimension d = button.getPreferredSize();
    maxWidth = Math.max(d.width, maxWidth);
    maxHeight = Math.max(d.height, maxHeight);
Dimension d = new Dimension(maxWidth, maxHeight);
...
voidexit(JButton aButton)
exit
aButton.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent event) {
        System.exit(0);
});
voidfixButtonWidth(JButton... buttons)
fix Button Width
fixButtonWidth(0, buttons);
voidfixToolbarButtonImpl(JButton button)
fix Toolbar Button Impl
ButtonUI ui = button.getUI();
String clazzName = ui.getClass().getName();
if (BUTTON_UI_CLASS_NAME.equals(clazzName)) {
    Class<?> uiClazz = ui.getClass();
    Method m = uiClazz.getDeclaredMethod("setRolloverDecoratedOnly", boolean.class);
    m.invoke(ui, true);
    m = uiClazz.getMethod("setRound", int.class);
    ClassLoader cl = uiClazz.getClassLoader();
...