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.

- 168,117
- 40
- 217
- 433

- 295
- 1
- 3
- 15
-
2from the look of it, you don't really need `GridBagLayout`. That can be done with a simple `GridLayout`. – ewok Feb 23 '12 at 18:53
-
do you not have to populate all the squares in the grid in that layout? will the text boxes not be all distorted e.g. the size of the squares? – Chris Mowbray Feb 23 '12 at 19:16
-
1You can use multiple JPanels with different layout managers. Your first panel can have a BorderLayout with a panel in the centre containing both forms and a panel south with the Start/Stop buttons. Then the centre panel can use a GridLayout with 1 row and 2 columns with each cell containing one of the forms in its own panel. Then you can use a SpringLayout or GridBagLayout for these panels to create the form structure. – Feb 23 '12 at 19:31
-
@Chris you can populate the cells with empty panels if you need fillers. – ewok Feb 23 '12 at 19:35
3 Answers
Probably a nested layout. For the 'groups of controls' on the top left and the right hand side, swap them all for a JTable
1 each or use GroupLayout
2.
1) JTable
2) GroupLayout
See also
- How to Use Tables
- A Visual Guide to Swing Components
- How to Use GroupLayout
- A Visual Guide to Layout Managers.
- Nested Layout Example (image below).

- 1
- 1

- 168,117
- 40
- 217
- 433
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.

- 50,182
- 6
- 67
- 111
-
http://s16.postimage.org/oh30otvgl/Layout.png This is the full image, as you can see there is already 2 panels within the JFrame, if i was to use the method you suggest above would there be problems accessing data from within the inner panels to the top panel? – Chris Mowbray Feb 23 '12 at 19:34
-
@Chris: I don't know what graphing software you're using, but the graph could be in it's own JPanel. A super JPanel using FlowLayout would contain the graphing JPanel and the main JPanel. – Gilbert Le Blanc Feb 23 '12 at 19:37
-
@Chris: What I would do is create a Financial model class that contains all of the values of the various text boxes. I'd have an Income class, an Outgoing class, and a Financial class that has a List of Income and a List of Outgoing. You would then pass an instance of the Financial class to however many JPanel classes you need to draw the GUI. The action listener on the start JButton would update the model, then animate the graph. – Gilbert Le Blanc Feb 23 '12 at 19:49
-
referring to your original answer Gilbert, can you set no. of rows and columns using gridbag layout? – Chris Mowbray Feb 23 '12 at 20:41
-
@Chris: You don't explicitly set the number of columns and rows. The GridBagLayout figures out the number of columns and rows from the GridBagConstraints that you use. One for each component. Don't reuse GridBagConstraints. – Gilbert Le Blanc Feb 23 '12 at 20:45
-
http://s13.postimage.org/94dt1ulif/Layout.png Got this far. the colors are to represent the two panels. How do I move the left side of items up and allign the text left? – Chris Mowbray Feb 23 '12 at 23:14
-
@Chris: I can't guide you through your project step by step. You can ask additional questions on Stack Overflow that anyone can answer. The GridBagLayout centers vertically. You can move everything up by having a large bottom inset on the last line. – Gilbert Le Blanc Feb 24 '12 at 13:10
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.

- 656
- 4
- 18
-
No using netbeans, trying not to go near the drag and drop GUI helpers as its for Uni and the code will look all over the place. Thanks anyway! – Chris Mowbray Feb 23 '12 at 19:26
-
Using netbeans pretty much kills it, but the reason I recommend WBP is because it does an excellent job of parsing hand written UI code and generates pretty clean code as well. – gorjusborg Feb 24 '12 at 16:25