Title: swing: appropriate layout manager for simple situation?
Tags: swing;layout
Question: I have a ```JPanel``` that I want to use to contain 3 vertical components:


a ```JLabel```
a ```JTextField```
a ```JScrollPane```


I want all 3 components to fill the ```JPanel```'s width. I want the ```JLabel``` and ```JTextField``` to use their normal heights and the ```JScrollPane``` to use the rest.

```BoxLayout``` almost works, except it seems like the ```JTextField``` and ```JScrollPane``` share the "extra" space when the ```JPanel``` is made large.

What can I do?


Here is the accepted answer: Create a BorderLayout. Put the JScrollPane in its center.

Create a JPanel with a BoxLayout. Put the JLabel and JTextField in that, vertically. Put that JPanel into the NORTH side of the BorderLayout.


Here is another answer: You could also use DesignGridLayout as follows:

```DesignGridLayout layout = new DesignGridLayout(thePanel);
layout.row().center().fill().add(theLabel);
layout.row().center().fill().add(theTextField);
layout.row().center().fill().add(theScrollPane);
```

This should exactly behave as you describe.

Each call to row() creates a new row in the panel.

The calls to fill() make sure that each component uses the whole available width.

A few advantages of using DesignGridLayout here are:


only one LayoutManager for the whole
panel
automatic borders and inter-rows spacing


Comment for this answer: thanks... when I'm willing to pull in a library, I use javabuilders + MiGlayout, this question was really about built-in layout managers, although I admit I didn't state that.

Here is another answer: GridBagLayout is pretty handy. You can control anything you need and you can control only what you need. You're probably going to be interested in only the vertical parameters.


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 3, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 2.5', 'CreationDate': '2010:00:02/12/10', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<swing><layout>', 'Title': 'swing: appropriate layout manager for simple situation?', 'ViewCount': 151, 'id': 2253441, 'user_AboutMe': '', 'user_AccountId': 19061.0, 'user_DisplayName': 'Jason S', 'user_Reputation': 180419.0, 'user_Views': 14288.0, 'user_id': 44330.0}}