Java – dynamic layout with extensible cells

I'm really stuck in a layout I have to make with Java swing

I have to build a dynamic form that iterates over both the label and the vector of the input component

How should it:

In this image, you can see what the result should be Between the green lines is a pair of label / input components I also get a constraint that tells me if I have to arrange them in one line 50 / 50 or two lines or one line 25 / 75 or 75 / 25 The whole table must also be adjusted, and the proportions of 50 / 50, 25 / 75 and 75 / 25 are still correct

I've tried gridbaglayout, which is very easy to use, but as you can see in the first line of the picture, the label may be very long So I used a jtextarea, but gridbaglayout cut it off I also like jgoodies formlayout, but I also have the problem of long textarea labels

The ratio of 25 / 75 and 75 / 25 is not important, but it is actually part of what I have to do

I think it's really difficult for textarea to extend its parent component

I also read / wrote these articles:

> 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 trapped

Do any of you have any ideas on how to do this?

thank you

Solution

I've done a lot of swing for some time, but I have two ideas:

>If the controller is cut off, it sounds as if you have not set the minimum size. > I don't think any existing layout manager can maintain the size ratio of 25 / 75 But don't despair! You can write your own layout manager Back when I did a lot of swing, I wrote several layout managers (I would say, "for special requirements" But they are actually quite common requirements, and I'm always surprised that Java doesn't include a canned layout manager to handle these things Love: you often want a row or a column of buttons, all buttons have the same size, so you have to find which button has the longest text, then adjust all other buttons to match it. Or: set rows and columns. The size of each column is suitable for the widest content in the column, but the width of one column has no effect on other columns; Then similar to line.) In any case, what you describe is quite unusual, so you may just want to write your own layout manager

The skill of writing layout manager is only to realize some key functions: (a) calculate the X and Y coordinates, and you will place each control given the overall size; (b) Calculate the minimum size required; (c) Calculate the preferred size; (d) Calculate the maximum size you will use Well, I may have forgotten one or two more things, but it's not a big deal When I first thought of "writing my own layout manager", it sounded like "writing my own database engine" or "writing my own compiler", but it wasn't that terrible It is usually hundreds of lines of code

Of course, it would be much easier if someone else here could tell you how to use one of the can layout managers to do what you want

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>