trouble with layouts

Joe Panully

Greenhorn

Posts: 22

posted 12 years ago

I'm having trouble with layouts. I've only ever messed around with flow and border layouts, but neither one are coming through for me on what i'm working on now. Basically, I want to have a button taking up the entire top of my gui, then a text label and a text field on the next row below that button to the left of it, and a text label than text field on that same row but to the left, then I want to have a label in the center of the next row and a text area under that.

There are a few ways you could go about this. You could use a GridBagLayout for a single JPanel and place all your components with it. However, GridBagLayout can be quite complicated although it is the most robust and flexable.

Another option is to split your frame into several different Panels. So for example...

Green - This is the main panel which will have a border layout. You would place a JButton on BorderLayout.NORTH.

Red - This is another panel. This is placed on the green panel at BorderLayout.CENTER. Give this a BorderLayout as well.

Yellow - Put this panel on Red at BorderLayout.NORTH and give it a flow layout or a GridLayout of 4 columns and 1 row.

Pink - this goes on the red panel at BorderLayout.CENTER and give it a layout of borderLayout. Put label3 at BorderLayout.NORTH and the textfield at BorderLayout.CENTER.

This is no means the only way, but it should give you a good starting place.

Joe Panully

Greenhorn

Posts: 22

posted 12 years ago

How do you add one layout to another? I tried a couple of different ways and I can't get it to work.