Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: Expand JButton to fill Container

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)).
like image 494
user1205577 Avatar asked Dec 09 '12 23:12

user1205577


People also ask

Can JButton added to a container?

For example, class JButton in swing API is a button component and provides the functionality of a button. One or more components form a group and this group can be placed in a “Container”.

What does JButton do in Java?

The JButton class is used to create a labeled button that has platform independent implementation. The application result in some action when the button is pushed. It inherits AbstractButton class.

How do I update JButton text?

By default, we can create a JButton with a text and also can change the text of a JButton by input some text in the text field and click on the button, it will call the actionPerformed() method of ActionListener interface and set an updated text in a button by calling setText(textField.

How do I set the size of a GridLayout button?

You may add a label in the first and a JPanel in the second column. The JPanel will resize to cover the enter cell. Then you add a button into that JPanel, and set the size of the button by setPreferredSize.


Video Answer


1 Answers

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

like image 138
The111 Avatar answered Nov 25 '22 10:11

The111