Vitalij
Vitalij

Reputation: 4625

Setting size of JButton inside JPanel with BoxLayout doesn't work as expected

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?

Upvotes: 3

Views: 12643

Answers (2)

Zargoon
Zargoon

Reputation: 333

One of the big issues with layout managers is that they all adhere to different rules on how they set the size of their components. This is especially frustrating because swing really encourages you to use the "panels within panels" approach of having multiple panels using different layout manager embedded within each other.

For example, BoxLayout doesn't stretch components horizontally, but instead adheres to the "preferred width".

The solution? You can either continue to use the panels with panels solution and constantly be fighting layout manager, or you could use a 3rd party layout manager known as MigLayout.

MigLayout is really flexible, easy to use, and I think it's it far better than any of the default swing layout managers. Additionally, if you use something like the Google WindowBuilder, you can use MigLayout with the drag and drop gui builder, and still have direct access to the source code!

Upvotes: 3

staticman
staticman

Reputation: 728

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.

Upvotes: 2

Related Questions