Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

GridLayout and createEmptyBorder in java.awt.frame 1

Status
Not open for further replies.

aas1611

Programmer
Dec 14, 2001
184
DE
Hi,

I'm about to create a GUI using JFrame. I've come across an example using methods GridLayout and createEmptyBorder as in:

workPanel=new JPanel(new GridLayout(6,1));
workPanel.setBorder(BorderFactory.createEmptyBorder(10,10,10,40));

What do the integers in GridLayout(6,1) and in createEmptyBorder(10,10,10,40) represent?

I've read through the documentation in java.sun.com but I haven't found a genuine explanation.

A brief explanation will be very much appreciated. Or maybe someone can refer me to a right forum or website?

Thanks!

Andre
 
I'd recommend going to Sun's site to consult the API, it's more reliable.

Cheers,
Dian
 
Sorry, it was the first I spotted in my Google search. I didn't read the URL, but I know from experience that the constructor summary was correct. You are right though, of course, Dian. Always go to the source.

Tim
 
Thanks...

After I tried to work on it, I can't find a way to change the size of buttons I want to put, even though I've tried several numbers in createEmptyBorder. Is there a way to do this?

here is the code:
Code:
workPanel = new JPanel(new GridLayout(2,2));
workPanel = setBorder(BorderFactory.createEmptyBorder(10,75,10,75);
workPanel.add(button1);
...
workPanel.add(button4);
pane.add(BorderLayout.CENTER, workPanel);

The size of these buttons are of equal, but too big, depending on the size of the frame (I set the frame size to 480x380, it has to be that big).

Andre
 
The layout managers, such as the GridLayout, constrain the size of the contained components. That's the whole point of using them. So if you've got a big grid, any component in a cell of that grid will match the cell size.

This aspect of gui design is probably one of the trickiest in Swing. It can be a right pain. For maximum control of components, have a look at the GridBagLayout. It's not for the faint hearted, though. I think there is a post in this forum from last year which describes the use of it.

Usually, a nested set of JPanels, each using mixtures of BorderLayout, GridLayout and FlowLayout gets the job done adequately, but this can suffer from the obvious complexity of a nested object model.

Or you can use an IDE with a GUI designer such as that available for Eclipse, or JBuilder. They get you 80% of the way, with tweaks taking you the rest.

Tim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top