• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Help on using layout manager

 
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Should I use the null layout for this scenario?
 
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic