Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8972339
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:11:45+00:00 2026-06-15T18:11:45+00:00

I have a JButton that I would like to expand to fill the size

  • 0

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)).
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T18:11:46+00:00Added an answer on June 15, 2026 at 6:11 pm

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a JButton that I would like to change the background color of
I'm practicing writing MVC applications. I have a Mastermind game, that I would like
I have a class that extends JPanel with several buttons on it. I would
I guess this question that would have already been asked here. I searched but
I have a JButton that is constructed using an Action and this action has
I have a class that extends javax.swing.JPanel , it contains a single JButton. I've
I would like to have a JButton (with a folder icon image) inside a
I have some code that moves files around and would like to implement a
I have the following piece of code that I would like help with... public
I have this code: public class Window extends JFrame { public Window(){ ... JButton

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.