6

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:

  1. 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);
    
  2. 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

  1. Removing the LayoutManager (in the call to new JPanel), which according to Oracle defaults to BorderLayout.
  2. Adding the dimensions to the call as in the accepted answer (i.e. new BorderLayout(0,0)).
Community
  • 1
  • 1
user1205577
  • 2,388
  • 9
  • 35
  • 47

3 Answers3

7

Your second attempt should be working, but that does not mean your entire top level container will be filled by the JPanel that contains your JButton. When there are no other components in a BorderLayout container, the center component will expand to fill the entire container.

http://docs.oracle.com/javase/tutorial/uiswing/layout/border.html

If the window is enlarged, the center area gets as much of the available space as possible.

Can you post an image of what you're seeing when you run your app? Here is a working example below. One thing worth noticing is that every container all the way up to the top JFrame is also using BorderLayout. It's possible in your attempt that some upper level container is restricting the size of your JPanel, and therefore also the JButton inside.

frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JPanel panel = new JPanel();
panel.setLayout(new BorderLayout(0, 0));
JButton button = new JButton("I'm a button!");
panel.add(button);

frame.getContentPane().add(panel, BorderLayout.CENTER);

enter image description here

The111
  • 5,757
  • 4
  • 39
  • 55
4

BorderLayout is the simplest solution.

The problem you are probably having is how the parent panel is then being laid out. Adding the button to a panel that uses BorderLayout then adding the panel to a panel that uses FlowLayout won't result in the effect you are after.

enter image description here

public class ExpandButtonTest {

    public static void main(String[] args) {
        new ExpandButtonTest();
    }

    public ExpandButtonTest() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new JButton("I'm a big button"));
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
3

Seems fine to me when using BorderLayout:

enter image description here

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Test {

    public Test() {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel panel = new JPanel() {
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(300, 300);
            }
        };
        panel.setLayout(new BorderLayout());

        JButton button = new JButton("Button");

        panel.add(button);

        frame.add(panel);

        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Test();
            }
        });
    }
}

Please post an SSCCE to show specific problems.

David Kroukamp
  • 36,155
  • 13
  • 81
  • 138
  • 3
    David, a tip: Use alt+printScreeen to get a screenshot of *only* the active app. You don't think I go to the trouble of cropping those scores of images I post, do you? ;) See [How do I create screenshots?](http://meta.stackexchange.com/questions/99734/how-do-i-create-a-screenshot-to-illustrate-a-post) for more tips. – Andrew Thompson Dec 10 '12 at 01:13