I’m really stuck with a layout I have to make with java swing.
I have to build a dynamic form, while iterating through a vector of labels and input components.
Here is how it should:

In this image, you can see, what should be the result. Between the green lines is one pair of label/input component. I also get a constraint, telling me, if I have to lay them out in one row 50/50 or in two rows or in one row 25/75 or 75/25. Also the whole form must be resizable with the 50/50, 25/75 and 75/25 ratio still correct.
I tried GridBagLayout which worked quite nice, but as you can see in the first row in the picture, the label can be very long. So I used a JTextarea but GridBagLayout cut it off. I also treid JGoodies FormLayout, but also had trouble with the Textarea for long labels.
The 25/75 and 75/25 ratio is not that important, but actually it is also part of what I have to do.
I think it is just really difficult to make a Textarea expand its parent component.
I also read/wrote these aricles:
- How can I create a JTextArea with a specified width and the smallest possible height required to display all the text?
- How to implement dynamic GUI in swing
- Expand JList row height depending on content
but I’m still stuck.
Does any one of you has any idea on how to accomplish this?
Thanks
It’s been a while since I’ve done much Swing, but two thoughts:
If a control is being cut off, that sounds like you’re not setting the minimum size.
I don’t think any of the off-the-shelf layout managers have a feature to maintain a ratio of sizes like your 25/75 split. But don’t despair! You can write your own layout manager. Back when I was doing a lot of Swinging I wrote several layout manager. (I would say, “for specialized requirements”, but really they were for quite general requirements, and it always surprised me that Java didn’t include canned layout managers to handle such things. Like: you often want a row or column of buttons where all the buttons are the same size, so you have to find what button has the longest text, and then resize all the others to match it. Or: set up rows and columns where each colum is sized to the widest thing in that column, but the width of one column has no effect on other columns; then similarly for rows.) Anyway, what you’re describing IS fairly unusual, so you may just want to write your own layout manager.
The trick to writing a layout manager is just to implement a few key functions: (a) Calculate the X and Y co-ordinates where you’ll place each control given the overall size; (b) Calculate the minimum size required; (c) Calculate the preferred size; (d) Calculate the maximum size you’ll use. Umm, there may be one or two more I’m forgetting, but it’s not that big a deal. When I first thought about “writing my own layout manager” that sounded to me like “write your own database engine” or “write your own compiler” at first, but it’s not really anywhere near that scary. It’s typically a few hundred lines of code.
Of course if someone else on here can tell you how to do what you want to do with one of the canned layout managers, that’s surely a lot easier.