44

There are 8 layout managers in the Java library alone, and then there's a bunch of 3rd party products as well.

The other day, I tried to use SpringLayout and... well, it ain't working out for me. See my other question. So... if you have a Swing app to design, and you want your layout just so, what are your preferred layout managers? Any interesting/useful experiences to relate?

Update: Yup, sure I'm aware that different LMs are appropriate for different layouts. But I'd expect that most of you use 1, 2 or maybe 3 for most everything you do, or you swear by a single one that's so versatile you never need anything else. With enough responses, I'd expect to see something like a bell curve, with a peak on GridBagLayout or MigLayout (for example) and a long tail of a few stragglers using (say) BoxLayout or FlowLayout.

Hopefully, the distribution of responses will reveal some trends as to what people use most to Get Stuff Done


UPDATE and Summary

OK, after almost 2 days, MiGLayout is definitely out in front! Its fans will be happy to hear that it looks like this layout will be entering the "official" library soon.

GroupLayout, FormsLayout and TableLayout are relatively new and haven't gotten much exposure. Perhaps others will be as surprised to find out about them as I was.

Community
  • 1
  • 1
Carl Smotricz
  • 66,391
  • 18
  • 125
  • 167

14 Answers14

67

MiGLayout, no doubt. Honestly, it's the only Swing layout manager I know of that makes any sense.

The mere fact that there are 8 layout managers in the core JDK is a good sign that the Swing creators had absolutely no idea about what they were trying to do. This is not to trash the rest of the Swing - it's a good GUI toolkit, except for the layout managers.

Joonas Pulakka
  • 36,252
  • 29
  • 106
  • 169
  • +1 - that's why I like SO, your're always pointed to libraries and tools that can make your life easier :) Thanks for the link! – Andreas Dolk Dec 02 '09 at 12:04
  • 1
    If you're trying to do anything moderately complex, or you want layout to respect system-specific component spacing, MigLayout is absolutely the best layout manager I've ever seen. It's also quite easy to do simple things. The only other layout manager I can see any reason for using is CardLayout, as it provides something rather different from any other layout manager. – uckelman Dec 02 '09 at 12:14
  • 7
    Having eight implementations of an interface is not a sign of weakness! (Although there is no guarantee that there are no unrelated weaknesses.) – Tom Hawtin - tackline Dec 02 '09 at 13:11
  • 2
    Granted - it could also be an intentional design decision. However, given that the amount has *grown* to 8 little by little as each JDK was released, it looks just like an attempt to fix earlier bad implementations. – Joonas Pulakka Dec 03 '09 at 05:30
  • 17
    -1 Disagree 100%. Different layout managers for different purposes, which can be combined to do almost anything. There's nothing wrong with that, – Michael Borgwardt Dec 03 '09 at 07:17
  • 8
    Like different machine instructions are for different purposes and can be combined to do almost anything. There's nothing wrong with that, but I'll rather use higher-level languages to achieve the same effect with less effort. – Joonas Pulakka Dec 03 '09 at 07:58
  • 3
    I'm with Joonas here. Those layouts look like they build on experienced gained over time. Nothing wrong with that, but it means some of the older ones are less useful than they could be. – Carl Smotricz Dec 03 '09 at 08:55
  • And we have a winner (by sheer weight of agreement). Pardon me for taking so long to accept, it slipped my mind! – Carl Smotricz Dec 08 '09 at 22:30
  • 3
    After about thirty minutes of struggling with every single one of the 'default' layouts, I googled and found this question. +1 for an epic recommendation, I picked MiGLayout in about 3 seconds (compare to 30 minutes still not groking any of the default layouts...) – Chris Browne Jan 29 '12 at 23:28
  • @JoonasPulakka - I have never used Mig. But i have used border, grid and flow of swing. They are easy to use. All you need to do is add a component. Is Mig that easy ? Or do you have to make some house keeping/preparatory code before you actually get to add the components ? If not, then mig is not advantageous for situations where a simpler swing layout would suffice. – SuperStar Apr 03 '13 at 02:36
  • 1
    Does Mig have oracle styled tutorials with colored pictures and such for the not so gifted learners like me ? – SuperStar Apr 03 '13 at 02:38
  • I am not a MigLayout user, but I am well-aware of its power. The key difference between (i) MigLayout (and other powerful layout managers) and (ii) simulating its features with many simpler layouts, e.g., BorderLayouts, FlowLayouts, and BoxLayouts, is each simple layout requires a JPanel (or JComponent). In certain situations, this *can* be wasteful (in terms of lines of code and memory footprint) in complex dialogs and windows. – kevinarpe Jul 27 '13 at 19:28
  • At work we always use the ABsolute layout manager. Why does everyone dislike that layout manager. – Doug Hauf May 21 '14 at 18:09
23

All of them, in combination. That's the whole point. Each layout manager fulfills different requirements, and by nesting panels with different layout managers, you can achieve almost anything.

The "do everything in a single panel" layout managers like GridBagLayout and GroupLayout (and lots of 3rd party ones) have their place, mainly when you need components in different parts of the layout to align, but with a large layout, they generally result in a huge, hard-to-handle mess.

Michael Borgwardt
  • 342,105
  • 78
  • 482
  • 720
18

It depends on what kind of GUI you are creating. You might use just one or two of the simple layouts, or you might need to reach for a more advanced layout. My overall layout manager use would probably break down to something like this, although it would vary based on the project:

  • 65% GridBagLayout - The one layout that will get it done, no matter what you need to do.
  • 15% Box/BoxLayout - Great for quickly & easily sticking a couple components together.
  • 12% BorderLayout - Good for attaching a button panel or info panel to a content panel. I almost always use it to add content to a JFrame.
  • 3% FlowLayout - Useful for button panels, but not much else.
  • 3% CardLayout - Mostly useful in programs that display different content panels for different operational modes.
  • 2% Other layouts - It's very rare that I need anything else, but occasionally one of the other layouts comes in handy.

Once you get the hang of GridBagLayout, it's not that bad to write initially, but it's still not pretty to work with, or to debug later on. I tried MiGLayout for something recently and was disappointed to find that the MiGLayout actually ended up being more complicated to use than the GridBagLayout in that particular case.

Some people try to avoid GridBagLayout like the plague; but the truth is, there are some things that no combination of simple layouts will be able to handle. It's fine to split a GUI into panels for different logical sections, but I think if you're creating a whole bunch of unnecessary extra nested panels just for the purpose of positioning components, you clearly need to learn how to use a GridBagLayout (or other similarly advanced layout, like MiGLayout). You might get your GUI to look okay with a nasty mess of nested BorderLayouts and GridLayouts and BoxLayouts, but as soon as someone starts resizing your application windows and dialogs to be either smaller or larger than you originally designed them, your GUI will probably look horrible and your customers will start to form a negative opinion about your product since you couldn't get such a simple thing right.

Update: I've been using WindowBuilder in Eclipse for a while now, and it greatly simplifies working with many layouts, especially GridBagLayout. I used to spend a lot of time writing layouts by hand, but with WindowBuilder or probably any similarly advanced visual editor, you can create the layouts in much less time.

rob
  • 6,147
  • 2
  • 37
  • 56
  • +1, Thank you for a detailed and thoughtful answer! I agree about GBL but have recently come to love GroupLayout, which does a couple of things GBL can't. Recommended! Like you, I found MiGLayout *almost* great. – Carl Smotricz Dec 03 '09 at 08:52
  • `Box/BoxLayout` what is mean? – turbanoff Oct 10 '15 at 13:18
  • @turbanoff you can set a Container's layout to BoxLayout, which can be made to behave similar to FlowLayout's default behavior. Alternatively, you can directly create a vertical or horizontal Box as your container (rather than setting a JPanel's layout to a BoxLayout, for example). – rob Oct 10 '15 at 21:30
15

DesignGridLayout both looks great and is easy to use through fluent interface.

just look at example:

enter image description here

with just a few lines of clean code:

    layout.row().grid(label("Last Name"))   .add(lastNameField) .grid(label("First Name"))  .add(firstNameField);
    layout.row().grid(label("Phone"))       .add(phoneField)    .grid(label("Email"))       .add(emailField);
    layout.row().grid(label("Address 1"))   .add(address1Field);
    layout.row().grid(label("Address 2"))   .add(address2Field);
    layout.row().grid(label("City"), 1)     .add(cityField);
    layout.row().grid(label("State"))       .add(stateField)    .grid(label("Postal Code")) .add(postalField);
    layout.row().grid(label("Country"), 1)  .add(countryField);
    layout.emptyRow();
    layout.row().center().add(newButton).add(deleteButton).add(editButton).add(saveButton).add(cancelButton);
mantrid
  • 2,830
  • 21
  • 18
  • Thanks for the pointer to this interesting layout manager! I've bookmarked it. – Carl Smotricz Mar 16 '13 at 10:13
  • It's cool, but got bug, my panel get bigger after each windows resize – Hiep Dec 04 '13 at 22:21
  • Hi. Do you know if I can use DesignGridLayout and set it not to not occupy the whole panel? Or add a "space" between LastName TextField and FirstName Label? – nmartins May 13 '14 at 21:47
12

It depends which kind of layout you need, that's why you have 8 of them:

  1. BorderLayout, simple and quite useful to design normal content frames (with main content in the middle and helpers on sides).
  2. GridLayout, useful when you have a grid of objects that should be of the same size (buttons? textfields?).
  3. GridBagLayout, very flexible, needs some tweaking to be fine and it is quite verbose (but I raccomend it if you want to do things well).
  4. FlowLayout, useless.. not a very layout: just placing item one next to another.
  5. CardLayout, useful for tabs or subviews that must be switched.
  6. BoxLayout, never used it too much.. it should be a sort of enhanced FlowLayout but it's not enough flexible to be used intensively.
Jack
  • 131,802
  • 30
  • 241
  • 343
  • 1
    The key to `BorderLayout` is the center is "greedy" by default and auto-expands widgets within to consume all available space. All other slots are non-"greedy". – kevinarpe Jul 27 '13 at 19:31
10

GroupLayout is pretty decent. It was originally intended for use by GUI Builder applications but I've found it to be very straight forward to code by hand too.

ninesided
  • 23,085
  • 14
  • 83
  • 107
  • 2
    I've discovered "pretty decent" to be an understatement! It's VERY versatile and can do a few tricks GridBagLayout can't. Recommended! – Carl Smotricz Dec 03 '09 at 08:56
  • 2
    I second this sentiment. I find `GroupLayout` to be extremely versatile. It produces some great layouts that scale very well. In windows where the user can just grab the corner and resize, this produces beautiful results. I code this entirely by hand as well. – Erick Robertson Sep 27 '12 at 17:36
8

FormLayout, part of the JGoodies Forms package has been a workhorse for me. It's not perfectly flexible in that it works hard to make your design look good. I've used it for several years on dozens of projects and it keeps on producing good looking output quickly.

You specify your layout in human-readable text, then add the components. Done.

clartaq
  • 5,320
  • 3
  • 39
  • 49
  • to few discussions: http://stackoverflow.com/questions/tagged/form-layout bugs not recognized as such: https://java.net/jira/browse/FORMS-18 – Wolfgang Fahl Feb 18 '17 at 08:47
7

I usually use border layout with gridlayout, first i design ui on paper prototype like ;

alt text
(source: usernomics.com)

After that we can split screen to gridlayout on borderlayout. In this picture we see NORTH, CENTER, SOUTH part (BorderLayout elements) and every part's layout can be gridlayout or BorderLayout, it depends on you. Same Layouts can use one within the other.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
bora.oren
  • 3,439
  • 3
  • 33
  • 31
6

GridBagLayout. Does pretty much all that you need (kind of) and it's in the Java library. Admittedly it does need some help, and the API is terrible.

GroupLayout makes a real mess of your layout code. Okay, so most people's GUI code is a big ball of mud. But your's does not have to be! Perhaps a nice interface could be put onto this layout manager, but I suspect it might have to be cloned-and-owned.

I am not in favour of introducing external dependencies unless they are really necessary. Also a lot of the third-party layout managers use strings of data, which have all the usual issues.

Tom Hawtin - tackline
  • 145,806
  • 30
  • 211
  • 305
  • Yeah... I seem to be the only person in my team who understands GridBagLayout. It's still painful, though. I'm happy when I can average only 4 lines of code per component, but that's only for boring layouts. There's gotta be a better way! – Carl Smotricz Dec 02 '09 at 13:37
  • 1
    Hehe, mastering `GridBagLayout` gives you instant respect, almost everywhere. ;) – Bombe Dec 02 '09 at 14:42
  • 1
    Bombe: Back in JDK1.02 days it was practically the first thing you did. Kids of today! – Tom Hawtin - tackline Dec 02 '09 at 15:40
  • 2
    GridBagLayout may take a lot of code, but that's because it allows you so much customization that many of the other layouts don't. People say GridBagLayout is confusing, but I say that the odd sizing and positioning behaviors of all the other layouts used in nested panels is more confusing than just learning GridBagLayout. – Peter Dolberg Mar 09 '12 at 18:57
  • I have written a convenient wrapper class that uses GridBagLayout with a builder-pattern-class for creating GridBagConstraints - makes most components one liners but gives me the ability to customise! – xeruf Sep 13 '17 at 13:09
5

There is no real answer to your question except that: it depends. It depends on what kind of frame (form) you are trying to create. I am no Swing-guru, but created a couple of (moderately advanced) GUI's and have never had the need to touch the GridBagLayout manager. I have always been able to create my GUI's using a combination of "easier" layout managers. For example, you can give your frame the BorderLayout and then place another layout in the SOUTH of that BorderLayout.

Bart Kiers
  • 166,582
  • 36
  • 299
  • 288
  • I tried getting away with using only the simple layouts when I first started Java programming, but quickly found that this led to undesirable behavior when resizing the application window. – rob Dec 03 '09 at 19:43
5

Use IntelliJ IDEA with its GUI designer. Makes GridBagLayout easy.

http://madbean.com/anim/totallygridbag/

glebm
  • 20,282
  • 8
  • 51
  • 67
4

I only use GridBagLayout, especially when doing some complex interfacing, believe me GridBagLayout can do anything. I also recommended the FlowLayout, because it is easy to use, and understandable, good for putting a group of buttons. Actually I only use this two layouts since it is part of the JDK library as for the MigLayout I haven't tried it yet so for now can't recommend it for you.

Marko
  • 20,385
  • 13
  • 48
  • 64
jerome
  • 41
  • 1
3

There are following layout options available:

MiGLayout
TableLayout
GroupLayout
FormsLayout

Out of these above MigLayout is the most recommended one as it is swing layout manager. There others have not got much exposure.

Joy Malik
  • 31
  • 1
2

I'm a Swing neophyte, but in the course of writing my first Swing application, I've tried four different layout managers: FlowLayout, BoxLayout, GridLayout and GroupLayout. In my opinion, FlowLayout and BoxLayout seems most appropriate for laying out groups of components of similar sizes. For components of varying sizes, GroupLayout seems to be the way to go. Steeper learning curve than the other two, but definitely worth it. As for GridLayout, I'd argue that you can achieve the same results you would with that layout manager by using a combination of FlowLayout and BoxLayout -- and you'd probably have more control of the placement of your components. But maybe that's just me :)

Sheldon

Sheldon R.
  • 452
  • 2
  • 7
  • 26