I have a JButton
that I would like to expand to fill the size of a JPanel
that is holding it. I've tried doing this a few different ways, with no luck. Here are some attempts:
Manual size setting as recommended here - size of the button did not change.
panel = new JPanel(new CardLayout()); button = new JButton(); button.setPreferredSize(new Dimension(BUTTON_SIZE, BUTTON_SIZE)); panel.add(button);
Trying to use a
BorderLayout
to expand the button as hinted at here. Size did not change.panel = new JPanel(new BorderLayout()); button = new JButton(); panel.add(button, BorderLayout.CENTER);
I'm probably doing something incorrectly, so any help is much appreciated.
EDIT
Here is a summary of what resolved it. There are 2 things that worked
- Removing the LayoutManager (in the call to
new JPanel
), which according to Oracle defaults to BorderLayout. - Adding the dimensions to the call as in the accepted answer (i.e.
new BorderLayout(0,0)
).