Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I best lay out these components in Swing?

Tags:

java

layout

swing

What would be the best approach / LayoutManager for a JPanel that needed to display the components shown in the image? (Note that the fields should resize horizontally when the size of the window is changed, but not the labels).

I'm currently trying (and struggling) to do it with a GridBagLayout. Is this the correct approach or is there a better way? Should I be trying to divide + conquer the seperate sections maybe?

Any thoughts or advice would be gratefully appreciated. :-)

http://img261.imageshack.us/img261/2091/layouthb6.png

PS If you read this and there's no image, please bear with me whilst I fix it!

like image 907
cagcowboy Avatar asked Jan 09 '09 14:01

cagcowboy


2 Answers

I would look at nesting JPanels. It would be very easy to make each row a separate JPanel and then just stack the JPanels.

Otherwise you can use the GridBagLayout. That is the one I use for more control over where components are placed.

Using GridBagLayout you can layout a row by incrementing the GridBagConstraint.gridx and then resetting GridBagConstraint.gridx = 0 and GridBagConstraint.gridy++ to move to the next row.

like image 81
jjnguy Avatar answered Oct 04 '22 03:10

jjnguy


I would suggest a third party free layout manager. MigLayout comes to mind (check the example form at the bottom of the linked page), or JGoodies FormLayout, which is especially good at formslike these.

The standard layout managers are good, but whoefully inadequate for frequent use for screens like these (the new GroupLayout used by the Matisse designer in Netbeans wasn't built for nothing). The linked layout managers are a lot easier to use and maintain IMHO.

like image 25
Robin Avatar answered Oct 04 '22 02:10

Robin