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

  • Home
  • SEARCH
  • 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 6776375
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:01:16+00:00 2026-05-26T16:01:16+00:00

Recently, I have started doing UI development in Java; I used to do UI

  • 0

Recently, I have started doing UI development in Java; I used to do UI development in WPF.
Some things about Java way of doing things are confusing.

What I am trying to achieve is to set minimum size of a button.
Here is the simplified code:

public class MainGameView extends JPanel {  
    public MainGameView(DefaultController controller) {

        this.controller = controller;
        CreateUI();
    }

    private void CreateUI() {
        MenuPanel = new javax.swing.JPanel();
        StartGameBtn = new JButton("Start Game"); 

        // Creating menu
        MenuPanel.setLayout(new BoxLayout(MenuPanel, BoxLayout.Y_AXIS));
        MenuPanel.setPreferredSize(new Dimension(200, 200));

        StartGameBtn.setAlignmentX(Component.LEFT_ALIGNMENT);     
        StartGameBtn.setMinimumSize(new Dimension(200, 30)); 
        MenuPanel.add(StartGameBtn);
    } 
}

So to my understanding, if the container gets allocated 200 pixels as its width, it should accordingly allocate 200 pixels of width to the button. However button stays the same size. Am I missing something here?

  • 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-05-26T16:01:16+00:00Added an answer on May 26, 2026 at 4:01 pm

    The BoxLayout will not stretch a component horizontally, it will allow it to remain at it’s preferred width. You should use a layout manager that does stretch horizontally. For example, based on what it looks like you are trying to do, you could use the BorderLayout:

      MenuPanel.setLayout(new BorderLayout(0,0));
      MenuPanel.setPreferredSize(new Dimension(200, 200));
      StartGameBtn.setAlignmentX(Component.LEFT_ALIGNMENT);     
      MenuPanel.add(StartGameBtn, BorderLayout.NORTH);
    

    This puts the button at the top, maintains its natural preferred height, and stretches it horizontally to fit the MenuPanel’s width even if it is resized. You might also look at the GridLayout (configured with 1 column) which I believe will let you add multiple components in a vertical column and all the components will stretch to fit the entire width of the MainPanel.

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

Sidebar

Related Questions

I've been doing some WPF development for the last few months, and I've recently
I have recently started doing some ActionScript/Flex programming and I am... Surprised... By the
I'm very used to working in WPF, but I have recently started building websites
I've recently started doing some JSF work - before that I've always used PHP
I have recently started learning Java EE 6 and I could need some help.
I have recently started seeing user agents like Java/1.6.0_14 (and variations) on my site
I've recently started logging my errors through codeIgniter. Since doing so, I have noticed
I recently started picking up vi , going through some tutorials and trying to
I have recently started messing round with CodeIgniter and ran into problems when doing
Recently I have started playing with jQuery, and have been following a couple of

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.