Skip to main content
GameDev.net gamedev.net
🔒 Locked

[java] Am I missing something?

Started by iNsAn1tY Feb 23, 2005 at 11:04 AM 4 replies 1.3k views
Original Post
iNsAn1tY
iNsAn1tY
I've been using Java 1.4.2 for my university course. I'm on the verge of writing a big program in it for the first time, and as it's a group project and I'm the "tech guy", I have certain decisions to make. I want to get them right. However, one of these decisions is giving me a bit of a headache: whether or not to use LayoutManagers. We have to create a GUI with the Swing library. I have tried to use LayoutManagers, but they seem next to useless. I honestly can't imagine why you'd want to have your controls laid out in the various bizarre styles the managers encapsulate. It seems counter-intuitive, and contrary to every other GUI creation system I've ever encountered. I've resorted to just turning off the LayoutManager in all my frames, and setting the positions of things manually. This has led to problems. You can't autosize controls to accommodate different font sizes or string lengths, for example. I've had to resize them manually, guessing their sizes. I did have a problem with detecting when frames I'm using change size, in order to recalculate the layout of the controls in them, but I've solved that by employing a ComponentListener to get resize notifications. These problems are causing me to ask the question: am I missing something with LayoutManagers? The GUI we've designed and the LayoutManagers don't seem at all compatible (and it's a pretty standard GUI, you'd have no trouble putting it together with Visual Basic or C++). Do I have no choice but to continue manually calculating and recalculating? Are there some situations in which LayoutManagers are wholly unsuitable for the task in hand? Thanks in advance for any replies...
My opinion is a recombination and regurgitation of the opinions of those around me. I bring nothing new to the table, and as such, can be safely ignored.[ Useful things - Firefox | GLee | Boost | DevIL ]
Lyrrwen
Lyrrwen
Well, layout managers can be tricky to get used to, and frustrating when they don't do exactly what you want, but I have found that overall it is easier to use them than do the work yourself. If the standard layout managers don't seem to work for you, try searching google for others. Quite a few people have made their own, and share them freely.

Apart from that, something many people seem to forget about is that you can use layout managers within layout managers. If you decompose your layout into smaller panels, you can get just about any configuration.

In case you haven't seen it yet, I found this guide pretty useful:

http://java.sun.com/docs/books/tutorial/uiswing/layout/visual.html
INsanityDesign
INsanityDesign
i would agree to my predecessor...

use layoutmanager and learn layoutmanager... i know myself how annoying they can be (believe me) but you can do so much with them, and definitly with using them together... look for some articles and go through every forum, there are many tricks and goodies how to use them...
also search in google for other layoutmanagers as descriped... there are some good... look on sourceforge for example...

i had a pretty hard fight with layoutmanagers, but i wouldnt wanna miss them... they are not perfect, far from it, but used right and a basic knowledge of their faults you can pretty do everything...
-----The next statement is not true!The previous statement is true!
CaptainJester
CaptainJester
Sun has a very good tutorial on how to use LayoutManagers: http://java.sun.com/developer/onlineTraining/GUI/AWTLayoutMgr/shortcourse.html

After I did that tutorial, I could make anything with LayoutManagers with ease. The tutorial is old, so it uses AWT, but LayoutManagement hasn't changed at all. They have only added a few more since that tutorial was written.

I have also found that I can do any layout I want with Java's built in managers. I have never needed a custom manager.
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][
iNsAn1tY
iNsAn1tY
Quote:
Original post by Lyrrwen
..something many people seem to forget about is that you can use layout managers within layout managers. If you decompose your layout into smaller panels, you can get just about any configuration.

Seems like that's what I was missing: nesting.

Quote:
Original post by CaptainJester
Sun has a very good tutorial on how to use LayoutManagers: http://java.sun.com/developer/onlineTraining/GUI/AWTLayoutMgr/shortcourse.html

From the glance I gave it, that looks like an excellent article on nesting LayoutManagers. I'll read it thoroughly later tonight.

Thanks for your replies, everyone [smile]
My opinion is a recombination and regurgitation of the opinions of those around me. I bring nothing new to the table, and as such, can be safely ignored.[ Useful things - Firefox | GLee | Boost | DevIL ]
Captain Logic
Captain Logic
You have to remember that Java is multiplatform - how can you ensure your GUI looks reasonably good on Windows, Mac, Unix or whatever? If you hardcode your GUI layout for Windows it will look crap on other platforms. That is why layout managers were included in Java. The layout manager ensures that the GUI will still be useable with different font sizes and widgets etc.

My recommendation is to forget the Swing layoutmanagers and use the JGoodies FormLayout from www.jgoodies.com. This is a free layout manager that "makes the simple things easy and the complex things possible". Unlike GridBagLayout for example. Just check out the demos at the JGoodies site and read the whitepaper if you're not convinced. With FormLayout you can create professional looking, precisely aligned dialogs that grow correctly when you resize the dialog.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.