File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Layout Managers and preferred size Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Layout Managers and preferred size" Watch "Layout Managers and preferred size" New topic
Author

Layout Managers and preferred size

Jennifer Sohl
Ranch Hand

Joined: Feb 28, 2001
Posts: 455
Hi. I am having a problem laying out a gui. I want to place five buttons and five labels on a panel. I want the button to have a corresponding label. I can do this with Grid Layout. However, I want the buttons to be small. Grid Layout will not let me set the size of the buttons. What would be the best way of going about this?
I'm pretty new with layout managers so bear with me (I was using visual age before to do GUI!).
Thanks a million!
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Hello,
One thing you might want to consider is explicitly placing components where you want them of the height and width you want them.

setLayout(null);

and then look at the Component.setBounds() method to set the location, height, and width.

Good Luck,
-Dirk Schreckmann
[ February 19, 2002: Message edited by: Dirk Schreckmann ]

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Jennifer,
The beauty of java is that you can do it many different ways. See the code below to see how to do it using two different layout approaches.

Enjoy,
Manfred.
Paul Stevens
Ranch Hand

Joined: May 17, 2001
Posts: 2823
Why do people always want to cop out and suggest and use null layout?
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Hello,
Did you feel like making an intelligent argument to not know about setLayout(null)?
[ February 19, 2002: Message edited by: Dirk Schreckmann ]
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
Null layouts totally loose out on the dynamic resize features of layout managers.
I have never worked on UI code that at some point did not need some kind of modification. If you hard code every position and size on your screen, you have a TON of extra work to add a button here or a combo box there.
Don't use null layouts. Learn to harness the power of LayoutManager!!!


Rob
SCJP 1.4
Paul Stevens
Ranch Hand

Joined: May 17, 2001
Posts: 2823
I have yet to see a UI that has to use the null layout. Once you use it, you also have to set the resizing of your window to off. If you don't, you have to place a great deal of code into your app just to make it appear proportional to where you started. The Layout Managers provide all of this functionality for you. The argument has been made many times in this forum against the use of null layout. It is a broken record. The difference between the null layout and the others is that it takes time to learn the other layouts and how they can be combined to do whatever you want. We can also talk about adding new components to an existing layout. Using the null all components need to be repositioned manually. With the other layout managers you may just need to add the component. If you need more argements against the use of null layout, just search this forum. There are plenty against.
Now why don't you provide an argument for their use.
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi Jennifer,
You can also get what you want by creating a custom component. Try the following, it produces 5 10x10 labeled buttons.

Hope that helps.
[ February 20, 2002: Message edited by: Jane Griscti ]

Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Layout Managers and preferred size
 
Similar Threads
Grids,tables,reports in Javascript
getPrefferedSize setLayout(null) 0 0
Grid layout problem
GridLayout program errors
2 panels in south region