Chris Mowbray
Chris Mowbray

Reputation: 295

Java GridBag layout

I am having a problem using GridBagLayout, I cant seem to get a hang of it. below is what my GUI should look like, I cant seem to get it working. The main problem is lining the text boxes & labels at the right & left hand side.

Desired Layout

Upvotes: 1

Views: 636

Answers (3)

Andrew Thompson
Andrew Thompson

Reputation: 168815

Probably a nested layout. For the 'groups of controls' on the top left and the right hand side, swap them all for a JTable1 each or use GroupLayout2.

1) JTable

2) GroupLayout

GroupLayout

See also

  1. How to Use Tables
  2. A Visual Guide to Swing Components
  3. How to Use GroupLayout
  4. A Visual Guide to Layout Managers.
  5. Nested Layout Example (image below).

Upvotes: 2

gorjusborg
gorjusborg

Reputation: 656

If you are using eclipse, I'd suggest using Window Builder Pro to get the UI where you you want it. You'll be able to see all the 'dials' you can turn with Gridbag layout.

You'll probably need to play with how things are anchored to get the labels to line up how you want.

Upvotes: 1

Gilbert Le Blanc
Gilbert Le Blanc

Reputation: 51445

I would have a main JPanel that uses the BorderLayout.

Then I would have two subordinate JPanels, one added to the main JPanel using BorderLayout.WEST, and the other added to the main JPanel using BorderLayout.EAST.

The west JPanel would use the GridBagLayout, 4 columns and 4 rows. I would use Insets to get the spacing that I want.

The east JPanel would use the GridBagLayout, 4 columns and 6 rows. Again, I would use Insets to get the spacing that I want.

I would put the buttons inside of a JPanel that uses FlowLayout. I'd set the preferred size of the JPanel so the buttons stay on the same row. Then I'd add the button JPanel to the east JPanel as the 6th row.

Upvotes: 1

Related Questions