Tomáš Zato
Tomáš Zato

Reputation: 53307

Instead of equal width, could elements in my GridLayout take only as much space as needed?

I have such JPanel:

container = new JPanel();
container.setLayout(new GridLayout(1, 1));

In this panel, I put JLabel and JTextField to create classical form. It renders like this:

image description

Obviously, I'd prefer to have the first column shorter, specifically just as wide as necessary to fit all JLabels. Like this (mspaint figure):

image description

The gray dashed line indicates where the two columns are divided. What do you propose for this layout?

Note that the second column may also contain check box, select box or some other form element.

Upvotes: 1

Views: 1204

Answers (1)

Andrew Thompson
Andrew Thompson

Reputation: 168845

Instead of equal width, could elements in my GridLayout take only as much space as needed?

No, it is designed to give elements equal width (and height). If the UI needs different widths, use another layout like GroupLayout or GridBagLayout.

See this answer for an MCVE of doing it using GroupLayout.

Upvotes: 1

Related Questions