views:

1690

answers:

3

I'm relatively new to java (specifically swing) and have recently been making some fairly simple applications, learning as I go. The latest one has lots of form components such as JLabels, JTextFields, JButtons, etc etc. All were placed in NetBeans using a visual builder. I have to say I'm not really taking to NetBeans and have really just been designing the GUI and copying the code straight to Eclipse.

My question is: Is it worth getting a very good understanding of GroupLayout and coding by hand (and therefore having more control over my GUI) or just continuing on as is?

+8  A: 

GroupLayout wasn't really designed for hand-coding; it's much, much more verbose than any other layout manager (except possibly GridBagLayout). Since you say you're new to Swing, I suggest starting with the basic layout managers (BorderLayout and FlowLayout); you can use them in NetBeans by right-clicking the panel or frame and choosing "Set Layout". After you're familiar with those, I suggest you check out MiGLayout.

To answer your other question: I think of NetBeans form designer as similar to a calculator; it's a wonderful tool if you already know the basics, but you'll never learn anything if you just use it as a crutch.

Michael Myers
I think SpringLayout is even more verbose. ;) I've used the GroupLayout that comes with LWUIT and it was not too bad, but not easy either. I liked JGoodies FormLayout, and since MiG Layout is its successor, I warmly recommend MiG. Anyways, it's good to know also the basic layouts.
Esko Luontola
I am already familiar with BorderLayout - but practically GroupLayout is what I think is best for the application I'm currently building. As for using NetBeans as a crutch - I merely don't like the IDE and find it needlessly over-complicated in some parts
Ciaran
+6  A: 

I'd say it's worth taking some time to understand GroupLayout if only because it's always a good idea to understand what your IDE is doing. Knowing what goes on behind the scenes actually makes you more flexible, because if you wind up in a situation where you don't have access to Netbeans, you'll know how to replicate the layout it would have produced for you. Besides, after you've gotten a decent amount of experience, you'll probably wind up saving time overall. You can often develop a simple GUI faster by hand than by using a visual editor, especially considering the time it takes you to copy and paste the code from Netbeans to Eclipse.

And of course, learning how to use GroupLayout by hand will also make it easier for you to transition to any of the other layout managers Java offers, which in turn can lead to simpler code and even more time saved.

David Zaslavsky
I'm thinking this too; I may be in a situation where I don't have the convenience of copying code from NetBeans but where I need to make a relatively complex GUI -- and in the cases where I need to make a simpler GUI, I need not turn to NetBeans, rather build it on a simpler layout manager.
Ciaran
+2  A: 

Most of the more complicated layout managers are not meant to be hand-coded. You can do it, but you'll likely have trouble understanding your own layout a few months from now. GroupLayout is no exception, worse it's not intuitive at all, you have to contort your mind forcing your layout into GroupLayout terms.

It's my opinion that these layout managers are not worth learning. GridBagLayout is the worst of all. It has more options than you can possibly figure out, and they never seem to do what you think they do. MiGLayout is very powerful, reasonably intuitive, and mostly does what you think it does, but I'll still argue that it's too powerful and too complicated for its, and the programmer's own good. GroupLayout is not as powerful, not as intuitive, and not worth the trouble.

My advice from years of laying out Java GUI and maintaining them is to learn and use the most powerful layout manager you can master in two hours and that you'll never forget, then layout your GUI with nested containers using this layout manager and the basic BorderLayout/GridLayout/FlowLayout/BoxLayout.

jtplumber
http://madbean.com/anim/totallygridbag/
Kevin Panko
-1 GroupLayout seems very intuitive to me. Sure, it's verbose, but this is Java we're talking about. Just read the documentation, and follow [this example](http://download.oracle.com/javase/tutorial/uiswing/layout/group.html). After that, start using it on "real life" GUI projects. I think you'll find it's really not as bad as it looks at first.
bcat