1

The following code describes a button that is instantiated in a JPanel with a BoxLayout in Page Axis:

private class AddInputSetButton extends JButton {
        public AddInputSetButton() {
            super("+");
            setMinimumSize(new Dimension(100, 100));
            pack();
            addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    addInputGroup();
                }
            });
        }
    }

I have tried setSize(), setPreferredSize(), and setMinimumSize() to no avail, none of them resize the button. I am still relatively new to Java GUIs, so hopefully it is something simple.

How do I adjust the size of the button?

EDIT: After further inspection, setPreferredSize() changes the size of the JPanel containing the buttons to the right size, but the buttons remain the same size.

El Capitan
  • 27
  • 1
  • 7
  • Can you please clarify your problem and what should occur? I'm unclear as to whether your EDIT means you've solved your problem or not. – Paul Samsotha Feb 12 '14 at 17:31
  • We also need to know what layout manager is used for the button's UI container. – arcy Feb 12 '14 at 17:46
  • A http://stackoverflow.com/help/mcve could certainly help here. Apart from that: Extending a JButton ONLY to set a few properties in the constructor is questionable. You might consider replacing this by a method that creates a standard JButton, configures it as desired, and returns it. – Marco13 Feb 12 '14 at 17:52

1 Answers1

0

JButtons (and a few other components) can be a bit goofy in layout managers. The layout manager is noticing that your button has a preferred size that needs to be respected, so it's adjusting your pane to accommodate. But your JButton is happy doing it's thing (what it thinks is right) unless you really force it to consider the size it's supposed to be.

If you're manually sizing your button (which isn't necessarily recommended), I'd say you should set all three properties (Minimum, maximum, and preferred). Maximum is the key - it forces the button to consider the other two sizes.

Here's a simple example that should work.

import java.awt.Dimension;
import javax.swing.*;

public class ButtonSizes {

    private static class AddInputSetButton extends JButton {
        Dimension d;
        public AddInputSetButton(int width, int height) {
            super("+");
            d = new Dimension(width, height);
            setMinimumSize(d);
            setMaximumSize(d);
            setPreferredSize(d);
        }

    }

    public static void main(String[] args) {
        Box buttons = Box.createVerticalBox();
        buttons.add(new AddInputSetButton(100,100));
        buttons.add(new AddInputSetButton(200,200));
        buttons.add(new AddInputSetButton(300,300));

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setContentPane(buttons);
        frame.pack();
        frame.setVisible(true);
    }
}
Nick Rippe
  • 6,465
  • 14
  • 30
  • [This](http://stackoverflow.com/q/7229226/1702990) is a nice question regarding whether or not to use these methods in the first place. The point being that a Layout Manager doesn't *have* to care at all about the components reported sizes. – Sinkingpoint Feb 12 '14 at 21:22
  • I'm intimately familiar with these conversations. In this case, the Layout manager isn't to blame for the shortcomings, it's the JButton's painting. So that conversation isn't so applicable. And since it's being used internally to the Class, it's doubly not applicable. The point of the conversation is that those methods should be marked `protected` instead of `public`. – Nick Rippe Feb 12 '14 at 21:31