The Java Tutorials' Weblog
Layout Management: Use an IDE or Code by Hand?
Have you seen this cool video about the frustrations of coding a GridBag layout manager? We on the Swing tutorial team think this is a pretty accurate portrayal of how frustrating hand-coding a GUI can be, which is why in the latest update of the Swing trails we've decide to encourage the use of NetBeans as much as possible, especially where layout management is concerned. So, do you agree with this approach? Have you used NetBeans, or any other IDE, to create GUIs, and if so, what do you think? For any hand-coding purists out there, what arguments do you make for NOT using an IDE? Please let us know your thoughts!-- Stuart Clements
Posted at 10:09PM Jun 12, 2007 by The Java Tutorial Team | Comments[64]
Tuesday Jun 12, 2007
Posted by Daniel on June 12, 2007 at 10:43 PM PDT #
Posted by palmiche on June 13, 2007 at 12:48 AM PDT #
12 years ago I was already making reasonably functional GUIs with the few layouters provided by Tcl/Tk so personally I think the Swing team should be ashamed that after so much time they haven't been able to come up with something that's just simple to use and that will work most of the time.
Matisse is really nice, but it just plain sucks for dynamic GUIs where you need to add/remove/change things at runtime (which is also where most (all?) GUI builders fail miserably).
Posted by quintesse on June 13, 2007 at 11:13 AM PDT #
Posted by anoweb on June 13, 2007 at 12:13 PM PDT #
Posted by Mikael Grev on June 13, 2007 at 12:25 PM PDT #
I think I have used GridBag maybe twice in 3+ years. Usually Border, Box, or Grid are enough.
Personally, I feel if you know all the layouts and their nuances then you can see how the layout will work prior to running your code. Eventually, something has to be hand coded, because no GUI creator can handle everything.
The only major problems I have had are with the selective auto-sizing of some layouts. Sometimes it seems that if you resize your window one time it expands one way and then you do the same thing and it does something else.
Posted by anon on June 13, 2007 at 12:26 PM PDT #
Posted by Mikael Grev on June 13, 2007 at 12:27 PM PDT #
Developers should not be required to work with Netbeans to learn swing.
Posted by P.Campbell on June 13, 2007 at 01:18 PM PDT #
This is a pretty accurate portrayal of how frustrating hand-coding a GUI can be when using the built-in layout managers, particularly GridBagLayout.
The deficiencies of GridBagLayout are well known: hard to learn, unnecessarily verbose, hard to update layouts, hard to understand layouts, etc. The other built-in layout managers are not advanced enough to create even moderately complex layouts with decent component sizing and re-sizing behavior.
The good news is that hand-coding layouts can be just as easy as using a GUI builder (without any of the drawbacks of GUI builders) provided that you have a decent layout manager. Back in the early days, I had good success using a modified version of TableLayout with Java 1.1 / AWT. I now use JGoodies Forms, which has excellent functionality and ease-of-use. Although I have not tried it, MiG Layout looks like it could be a worthy all-in-one successor to JGoodies Forms.
Posted by Daniel on June 13, 2007 at 02:08 PM PDT #
public class ContactViewPanel extends ViewPanel implements ContactViewConstants { public ContactViewPanel() { super(FORM_CONTACT_VIEW); new ContactController(this, new ContactModel()); } }What would formerly could have been potentially several hundred lines of UI related layout code is reduced to a view consisting of ~5 lines of code. Benefits include:- Obviously eliminates the need to write layout management code when creating UIs. Consequently, depending on the complexity of a particular UI, the amount of code written can be reduced in some estimates by up to 30-40%. This translates to time "re-acquired" which can in turn be used more effectively to code business specific logic.
- Since utilizing layout managers such as GridBagLayout can be extremely time consuming and error prone. This design approach eliminates this risk.
- UI work related to the view becomes trivial and can be performed by someone with little or no background in Swing.
- If later on screen layout requirements change and usability dictates a different component flow, only the view needs to be changed (using a WYSIWYG designer such as Abeille or JFormsDesigner). No application code need be modified.
Anyone interested in example code feel free to drop me an email. ~ToddPosted by Todd Viegut on June 13, 2007 at 03:06 PM PDT #
Disagree. Here are some arguments from a hand-coding purist:
First of all, GridBagLayout is not a good example, because afaik it has also been designed with tool support in mind, but nobody was ever able to deliver such a tool.
Second, never tie your project to a particular IDE. Ironically, I thought that the NetBeans people had this lesson already learned, when they redesigned the whole build process around Ant (hobbyists may rely on an IDE to build their project, professionals prefer automated nightly builds, typically on headless machines ;-)
Next: At the time of EJB 1.0, everybody said: "Well, those XML-descriptor files are a pain, but hey, the IDE-guys will give you the tools you need!" We know what happened. JEE 5 is all about making coding simpler and more straightforward. Lesson learned: Improve your architecture to get rid of unnatural artifacts, instead of throwing tool support at the problem.
Last point: On the server-side, the JSF people have quickly realized, that tool support is not the holy grail, although there was plenty of effort (Studio Creator). Quote from the new JSR-314 (JSF 2.0): "The act of writing JSF applications and components by hand will be made much easier by this JSR."
Dear Swing folks, I think you're moving in the wrong direction...
Posted by Karl Peterbauer on June 13, 2007 at 04:46 PM PDT #
Posted by Shanon on June 13, 2007 at 05:08 PM PDT #
Posted by Aron on June 13, 2007 at 05:37 PM PDT #
Posted by nile black on June 13, 2007 at 06:39 PM PDT #
Posted by Adam on June 13, 2007 at 06:41 PM PDT #
Posted by Chinh Nguyen on June 13, 2007 at 07:26 PM PDT #
Posted by Viswanath on June 13, 2007 at 07:39 PM PDT #
I've been doing desktop apps for the last 11 years, and I started coding in Java 2 years ago. I'm used to creating GUI's both by hand and with visual designers, but I much prefer the visual way. The NetBeans GUI Builder, Matisse, is awesome, and was the main reason for my switching to NetBeans. My first Java IDE was JBuilder, and the GUI builder was only passable. If you had a good idea of how you wanted your form to look like, and did everything right right from the start, it was OK. However, if you wanted to change something in the overall layout, you'd have a hard time putting everything in its proper place. Then I tried Eclipse, coding my GUI's by hand, but that was plain unproductive. Next I tried NetBeans, I've been using it for a year now, and it's been a great experience. I never needed to tweak the code it generates for my forms. (Later I found out that Eclipse users who'd like to try Matisse can use MyEclipse, it works just as fine.)
That said, one of the beautiful things about Java is the great ecosystem it has managed to build around itself, so you generally have several choices to get your work done. If you want to hand-code your GUI, that's OK. If you want to load your GUI from an XML file, that's OK too. If you want to build your GUI visually, using either NetBeans, JFormDesigner, RADi or something else, all of them work great. So, my point is, make it clear in the tutorials that there are several choices, and try not to overemphasize any specific tools. I'm sure people will be able to find out for themselves the solution that best suits them.
Posted by Roger Araújo on June 13, 2007 at 07:57 PM PDT #
Posted by René Dolezal on June 13, 2007 at 09:02 PM PDT #
Posted by Paul Gear on June 13, 2007 at 10:16 PM PDT #
Posted by Mike Urban on June 14, 2007 at 01:24 AM PDT #
Instead focus on creating a powerful yet simple to use layout! Current layout managers are powerful but often way too complex to code. Focus on how a UI designer wants to create a UI and try to hide implementation details to deal with platform independency as much as possible. When creating a UI you don't think in grids, flows, let alone in springs as in springlayout, nor indents, nor gaps):
1) You simply want to place components on a certain position (just as you do in a RAD tool wit DnD);
2) For components you want to specify stretch: horizontal and/or vertical
3) Provide an advanced option to place components "aligntop", "alignbottom", for instance for a toolbar or statusbar.
When a component resizes components to the right or bottom should move accordingly (to some most-logical rule, if you want something different use the powerful yet complex existing layouts).
And while we are at it, it would really be nice if we could just specify a "taborder" property for components to determine the order in which components are tabbed instead of having it dependent on add order or writing a ContainerOrderFocusTraversalPolicy
kind regards,
Christiaan
Posted by Christiaan on June 14, 2007 at 02:18 AM PDT #
Understandig the code you write is the best way to less frustration, IDE layout is only avoiding one problem just to step on another right after it.
Posted by Davor Hrg on June 14, 2007 at 03:00 AM PDT #
Posted by Davide Raccagni on June 14, 2007 at 04:13 AM PDT #
Posted by Andriy Tsykholyas on June 14, 2007 at 05:38 AM PDT #
Posted by Robert Burkhead on June 14, 2007 at 06:15 AM PDT #
TOOLS ARE DISPOSABLE, CODE IS FOREVER
We need a new layout manager that is easy to code and read, end of story.Posted by aberrant on June 14, 2007 at 06:36 AM PDT #
Posted by china li on June 14, 2007 at 06:48 AM PDT #
sure, GridBagLayout is a great layout manager, but it also has its problems. You can see one problem in the Totally GridBag video when he resizes the final version of the dialog and the two text fields become very very small. This is because if there is not enough space to give the text fields the preferred size, then GridBagLayout uses the minimum size (instead of using the available space as JGoodies FormLayout do).
Another weakness is that GridBagLayout does not support gaps between columns and rows. Sure, you can use insets (or empty columns/rows with minimum width/height), but this is IMHO a nightmare.
Definitely ;)
I don't think so. IMO it may become a nightmare to modify hand-coded GUIs. Particular if it was coded by other people or if it is several years old. You have to read and understand the code before trying to change something. When using a GUI builder, just open the form in the GUI builder and make the necessary change. Takes only minutes ;)
Posted by Karl Tauber on June 14, 2007 at 07:03 AM PDT #
Posted by Lorenzo Sicilia on June 14, 2007 at 07:32 AM PDT #
I am irritated by the repeated attempts by Sun to shove NetBeans in my face. The first example for every technology should be code. Just show me the code. It's icing if you want to show how to do that in an IDE that I don't use (actually, that's marketing -- and I'll treat it as such by ignoring it unless I'm shopping).
I think there is a misperception about tooling, that partly is from looking "over the fence" at Visual Studio marketing materials. Window's author Charles Petzold sums up the truth fantastically in his article Does Visual Studio Rot the Mind?.
Posted by Jason Sando on June 14, 2007 at 07:51 AM PDT #
Posted by Mike on June 14, 2007 at 09:08 AM PDT #
Posted by Joshua Marinacci's Blog on June 14, 2007 at 10:53 AM PDT #
Hand-coding a GUI can be frustrating and can be a joy. It depends on many factors: What do you want to achieve? Who designs and who implements? What layout manager do you use? Do you use a layout system on top of the layout managers? Do you want to comply with style guides? With one or many style guides? Do you want a consistent UI? Etc.
You seem to mix two different roles during the UI production: finding a design, and implementing it. I've found when working with teams, that many developers face problems in finding the design. Once they know how a screen shall look like, they can construct it efficiently with a decent layout system by hand, or with a decent visual builder tool.
I personally use builders to be able to benefit from Meta Designs - something you don't find in the available visual builder tools. Think about how you'd design and implement your local bus schedule. Do you use a tool to build every page individually - without any relation to the other bus lines? Likely not. Likely you start with a Meta Design that specifies the abstract layout, the fonts, positions, and some rules for the content. Then you just build the many pages by applying content to your meta design. This way you get well designed and consistent pages at affordable costs.
- Karsten Lentzsch
Posted by Karsten Lentzsch on June 14, 2007 at 11:10 AM PDT #
There are many other things where knowing the basics in programming really helps. For example how floating point numbers work, or being able to do assembler, being able to use a debugger (instead of brain-dead println). You won't use the knowledge every time, but when push comes to shove and deadline looms, you are glad you have one more trick in the bag instead of having to give a blank stare when the boss comes to your desk.
A note to Mr. Marinacci. Don't you have to fix one of the many age old Swing bugs heroes like you added to Swing? If yes, why are you hanging out here instead of running the debugger?
Posted by Sam on June 14, 2007 at 11:29 AM PDT #
Posted by Neil Weber on June 14, 2007 at 11:35 AM PDT #
The comment about the IDE GUI builders falling apart for dynamic GUIs is valid. At that point I usually need to do some rough layout by hand to establish the places where my IDE-designed panels will be dynamically placed.
Posted by SWPalmer on June 14, 2007 at 01:19 PM PDT #
Posted by Rick on June 14, 2007 at 06:24 PM PDT #
Posted by Raj Nagappan on June 14, 2007 at 08:15 PM PDT #
Posted by Carl on June 14, 2007 at 10:09 PM PDT #
Posted by Tobega on June 14, 2007 at 11:07 PM PDT #
Posted by Tobega on June 14, 2007 at 11:09 PM PDT #
Posted by Kieron Wilkinson on June 14, 2007 at 11:40 PM PDT #
Posted by JT Wenting on June 14, 2007 at 11:55 PM PDT #
As pointed out above you are hooked into a toolkit, spend effort in an area not giving major productivity gains and directs the focus into the wrong direction. For me tabular layouting (Karsten's FormLayout) and a relative layout (RelativeLayout) addresses almost all situations and are reasonable readable.
Not to blame here but I'm eagerly awaiting more productivity gains for my team in having better framework support on top of Swing. JSR-296 or things similar to Eclipse JFace would be very helpful. Having the 80% useful stuff working right out of the box but not hiding the power of Swing below.
Posted by Wolfgang Rostek on June 15, 2007 at 12:33 AM PDT #
Posted by asc on June 15, 2007 at 12:49 AM PDT #
Posted by Peter Cameron on June 15, 2007 at 01:44 AM PDT #
Posted by 207.236.24.133 on June 15, 2007 at 07:13 AM PDT #
Just kidding. Build the component in a UI builder or by hand and then remix it like this.
Posted by Jan Erik Paulsen on June 15, 2007 at 09:30 AM PDT #
Posted by varan on June 15, 2007 at 09:54 AM PDT #
Posted by varan on June 15, 2007 at 11:43 AM PDT #
Posted by Zhao Yi on June 15, 2007 at 11:48 AM PDT #
Posted by guru on June 16, 2007 at 09:11 AM PDT #
Posted by Hanrui on June 17, 2007 at 04:39 AM PDT #
Posted by Locky on June 19, 2007 at 01:17 AM PDT #
The tutorial should be useful independent of the IDE you are using.
This means it should provide you with the bits you need to know to be successful in laying out guis. This would include a basic understanding of core layout concepts and layout managers (hey, I still use BorderLayout and Box an amazing amount of the time), an awareness/pointers to the better 3rd party layout managers and tools, as well as optional trails on how to use netbeans/matisse. Ideally it should give the reader an inkling on how to choose the best approach for a given project.
Amy Fowler
recovering GridBagLayout addict :)
Posted by Amy Fowler on June 19, 2007 at 10:47 AM PDT #
Posted by Marco Alan Rotta on June 19, 2007 at 12:40 PM PDT #
Posted by maxwell on June 21, 2007 at 11:58 AM PDT #
Posted by korey on June 27, 2007 at 04:51 PM PDT #
Posted by LeO on July 03, 2007 at 02:41 PM PDT #
I also want to nth the sentiment that your statement "We on the Swing tutorial team think this is a pretty accurate portrayal of how frustrating hand-coding a GUI can be" should be a source of shame for the Swing team. The frustration of hand-coding a GUI comes mainly from the terrible layout managers that come with Swing. Even a brief encounter with jGoodies or Mig or XMFormLayout (for AWT unfortunately, based on the ancient Motif XmForm widget... so the Swing team can't say there were no good examples to look at) should embarass you guys back to the drawing board.
Posted by bayesianinitiate on July 07, 2007 at 12:22 PM PDT #
Posted by varan on July 08, 2007 at 08:54 PM PDT #
I will learni jbuilder 2007
Posted by kani on March 09, 2008 at 06:53 PM PDT #
Hi Todd,
Can you send me a an example to use forms(jfrm or xml)designed by Abeille Forms Designer in eclipse or netbeans.
Please mail to:kpraok@rediffmail.com
Thanks in advance.
Posted by Rao on April 06, 2009 at 12:10 PM PDT #