Question: I'm trying to make a layout that looks like this in java swing:

[![enter image description here](https://i.stack.imgur.com/QzfHH.png)](https://i.stack.imgur.com/QzfHH.png)

but none of the layout managers provided by swing fit this use case. In android I can simply use a linear layout and set the height of child elements to `wrap-content`, but swing doesn't have any corresponding feature. Swing layout managers tend to set the size of child elements themselves based on the constraints and this is causing issues with textfield.

How do I implement this layout in java swing?

Answer: 
Here is one way to approach it.

[![enter image description here](https://i.stack.imgur.com/UgZXz.png)](https://i.stack.imgur.com/UgZXz.png)
You can use 3rd party library

* Swan Layout <https://github.com/idayrus/swan-layout>

If you use gradle, add this to build.gradle

```
implementation 'com.github.idayrus:swan-layout:1.01.000'

```

for other build tools, see <https://jitpack.io/#idayrus/swan-layout/1.01.000>

Example in Kotlin:

```kotlin
val lc = LinearConstraints()
val panel = JPanel(LinearLayout(LinearLayout.VERTICAL))

lc.margin = 10
lc.width = LinearConstraints.MATCH_PARENT
lc.height = LinearConstraints.WRAP_CONTENT
panel.add(JButton("Hello World"), lc)

```