*
The moose likes Beginning Java and the fly likes Help on using layout manager Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help on using layout manager" Watch "Help on using layout manager" New topic
Author

Help on using layout manager

Rudy Yeung
Ranch Hand

Joined: Dec 27, 2000
Posts: 183
I need help on layout manager (Gridlayout and GridBagLayout). I try adding two labels and two text fields within a JFrame as follows:
First Name: ____________
Last Name: ____________
By default setting, they are placed in the center of the Frame. No matter how I play around with the setting, I am not able to make it left justified to the edge of the Frame. Can someone help me how to make it left justified with say 1 inch margin.
Rudy Yeung
Ranch Hand

Joined: Dec 27, 2000
Posts: 183
Should I use the null layout for this scenario?
Grant Crofton
Ranch Hand

Joined: Nov 08, 2000
Posts: 154
Originally posted by Rudy Yeung:
I need help on layout manager (Gridlayout and GridBagLayout). I try adding two labels and two text fields within a JFrame as follows:
First Name: ____________
Last Name: ____________
By default setting, they are placed in the center of the Frame. No matter how I play around with the setting, I am not able to make it left justified to the edge of the Frame. Can someone help me how to make it left justified with say 1 inch margin.

You just need to play with different layouts until it works. I'm not sure if GridLayout is what you want, and personally I wouldn't go near GridBagLayout with a 10ft pole. It provides the most accurate positioning, but it can be a nightmare to set up.
I think the standard layout is BorderLayout, and you can add stuff to the left with
add( BorderLayout.West, myLabel );
You will need to put your components in a panel first and add that to the layout if you have more than one. This panel could have a GridLayout or perhaps a FlowLayout.
Or you could set the layout to GridLayout with a few 'cells', and work out which ones should contain your components - you can fill the rest with blank labels. You can't do this if you have other things in the frame, unless you want them positioned in the cells.
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4340
    
    2

As Grant said, "You just need to play with different layouts until it works. "
Dont forget you can add panels(with their own layouts), thereby creating almost anything you want.
I think null layout is even more of a last resort than GridBagLayout.
------------------
Dont blindly believe everything I say.


SCJP
Visit my download page
Mindy Wu
Ranch Hand

Joined: Jan 12, 2001
Posts: 121
Hi Rudy,
I think I can help you out. Think physically about how would you like the layout of the components. A frame can have as many panel as you want. You can create panel within panel. You situation is not that bad.
I assume you have previously created the label and text box!
first create 1 panel for label.
//LAYOUT THE LABLES IN ONE PANEL
JPanel panLabel = new JPanel();
panLabel.setLayout(new GridLayout(0,1)); // row, column
panLabel.add(lblFirst); //add the label
panLabel.add(lblLast); //add another label
create another panel to hold the text box.
//LAYOUT THE TEXT FIELD
JPanel panField = new JPanel();
panField.setLayout(new GridLayout(0,1));
panField.add(txtFirst);
panField.add(txtLast);

//create a content panel to hold the above two panels
JPanel panContent = new JPanel();
panContent.setBorder(BorderFactory.createEmptyBorder(20,20,20,20)); //set the panel distance from the edge of the frame
panContent.setLayout(new BorderLayout());
panContent.add(panLabel, BorderLayout.WEST);
panContent.add(panField, BorderLayout.EAST);
And that it is! Let me know if you can make it. I hope I can provide further assistance. Good luck.
Mindy Wu
Rudy Yeung
Ranch Hand

Joined: Dec 27, 2000
Posts: 183
Mindy,
Thanks for your advice. It works. The key point in your snippet is the line:
"topPanel.setBorder(BorderFactory.createEmptyBorder(20,20,20,20));"
Actually, I have another solution for myself. Instead of using GridLayout or DGridBagLayout, I use flowlayout manager because it can control the left and right justification of the components against the edge of the frame. I use two panels. Each panel uses the flowlayout manager, which has two components (one label and one text field) filled in. The two resulting panels is then added to another panel in both the North and South borders. But I like the way you do it though.
If you had not told me, I would have never figured out the usage of the setBorder and the BorderFactory. BTW, do you know if there is any website talking about the tricks like the one I give. It looks very simple, but just do not know how to solve it.
Mindy Wu
Ranch Hand

Joined: Jan 12, 2001
Posts: 121
Hi Rudy,
I am glad to know that you made it. Ok, for more inforamtion about layout managers go the http://java.sun.com/docs/books/tutorial/uiswing/layout/using.html where talks about 6 different layout managers, and also it provides example for each of the layout manager.

Mindy
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Help on using layout manager
 
Similar Threads
Frames with tabs (make 2 frames ?)
JFrame, JPanel and sizes
add a Canvas to a JPanel within a JFrame
Jtext problem very urgent
Layout Manager