1

I am working with FlowLayout Manager in my JPanel. I have many Label and TextField/JComboBox combinations. like

enter image description here

When JPanel is resized

enter image description here

I have set FlowLayout on my JPanel and whenever the panel is resized then instead of taking components one by one to next row, I want it to take my Label+Textfield together to next row.

Is it possible to make of group or something like that so my each Label+Textfield are considered as single component and when the panel is resized both of these Components come down together.

One way I know is that put my every label+textfield in every seperate JPanel and then putting these JPanels in my main FlowLayout JPanel but then I will have to do alot of work therefore wanted to know if there is some option in java for this?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • *"wanted to know if there is some option in java for this?.."* `GroupLayout`. E.G. the factory method in [this answer](http://stackoverflow.com/a/21659516/418556) can produce a two column layout of labels and fields. Put two of those side by side, and the job's done. – Andrew Thompson Oct 13 '14 at 10:42
  • @AndrewThompson But I am using FlowLayout on my JPanel because Panel is resizeable where do you suppose to use GroupLayout then? –  Oct 13 '14 at 10:47

1 Answers1

0

Flowlayout is the wrong layout to use in your situation.

Probably the best layout is GridBagLayout. You can define a consistent width for your text fields, and a consistent anchoring for your labels (ie. anchored to the right).

Here's an example for how you would set your 'model' label :

GridBagConstraints gbc = new GridBayConstraints();
gbc.gridx = 2;
gbc.gridy = 1;
gbc.anchor = GridBagLayout.EAST
panel.add(modelLabel, gbc);
Oliver Watkins
  • 12,575
  • 33
  • 119
  • 225
  • Thanks Oliver. It worked great. As always you gave the great answer. Thanks alot. –  Oct 13 '14 at 11:27