I have been trying for years to master Swing layout managers with no success. Every time I need to do something with Swing, it is like pulling nails to get the visual components to line up the way I want them to. I am familiar with frameworks like Matisse but that's not the point.

My question is: Why does Swing even need layout managers? It seems like an overcomlicated paradigm. What I would like to be able to do is jave a simple JFrame and then for each component (like JLabel) simply specify X and Y coordinates where I want it to be situated -- done deal. Can anybody explain what is wrong with this simple approach that would work predictably 100% of times? Or is there an actual layout manager that does things exactly like this?

  • 4
    a) You can specify coordinates for your components in Swing. b) Unless your windows are a fixed size, this is a terrible idea because the components will never move or scale with the window. – user7007 Jan 18 '13 at 18:48
  • 1
    in that case the X,Y can be percentages on the X and Y coords so the position changes with the window size. E.g. if you wanted it to be in the middle, you'd set 50,50 etc. – amphibient Jan 18 '13 at 18:52
  • 2
    So what, top-left corner would be 50%, 50%? Wouldn't that look odd? How would you do sizes, then? With percents as well? Absolute positioning, which you can do, is terrible mostly because you have to consider how the thing will look at feel at every possible permutation of window size/screen resolution. It may end up looking great on your 1024x768, but wonky and stretched on my 1900x1280. – Roddy of the Frozen Peas Jan 18 '13 at 19:13
  • 1
    Try TableLayout. In my opinion it's much easier to use yet as powerful as for example GridBagLayout. – msell Jan 18 '13 at 19:17
  • @msell, that may be a solution for my frustrations. is that part of Google API? – amphibient Jan 18 '13 at 19:33

What I would like to be able to do is jave a simple JFrame and then for each component (like JLabel) simply specify X and Y coordinates where I want it to be situated -- done deal.

You can do that with null layout:

jLabel.setBounds(5, 5, 200, 300);
  • all you need about layout managers is to don't use one. – linuxunil Jan 3 '17 at 12:42
  • @linuxunil or gridLayout, it's pretty fine for most of the usages and isn't too hard :) – Walfrat Jan 3 '17 at 13:56
  • 3
    note : Why does Swing even need layout managers? this does not answer to the question. Even if OP accepted it ... – Walfrat Jan 3 '17 at 13:57
  • @Walfrat for both statements : indeed. :) – linuxunil Jan 3 '17 at 14:03
  • So long as you know the labels will never change. – Rob Crawford Oct 15 '19 at 21:26

Maybe you're too young to remember when programming without layout managers was more common. Either you had to fix your screen at one size, which worked great unless you actually needed to share your application with someone who used a different screen resolution, or you had to deal with scaling that looked really bad.

Part of the problem is what you might consider "natural" and "obvious" scaling is actually quite complex under the hood. Some components, like a text area, can naturally be resized in both directions and still look pretty good. Some components, like a combo box, only look okay when scaled in the horizontal direction, and some components, like buttons, rarely look natural when scaled more than a little bit in either direction. Layout managers let you mix and match components and still make the scaling look natural.


Other toolkits have equivalent concepts (e.g., Tk has geometry managers) as well, and they are absolutely central to having a GUI that is not fixed at one exact size. That matters when:

  1. you have fonts that can change in size for accessibility reasons, or
  2. you have different sizes of screens, or
  3. you have resizable windows, or
  4. you have variable amounts of content in some components.

That's not a complete list of reasons why you might want adaptive sizing, but it does illustrate that you may well not have control over the precise placement and sizing of things even if you might want it. Far better to stop trying to control things at the pixel level and to instead leave the computer to look after that for you following more general rules that you describe; what layout managers do is enforce those rules.

FWIW, writing a good layout manager is hard. Really hard. In fact, the one I know of that gives the best layouts in my experience (Tk's grid manager, which is a sort of souped-up GridBagManager except without the suck) is internally a complicated simultaneous equation solver with some scary heuristics to ensure that an answer pops out even when the user's directives don't give a solution. It's also had a few man-years of effort poured into its development (really!) so I wouldn't expect it to be reproduced easily. Because of the general difficulty in doing layout managers well, most people who write them tend to write very specific ones that don't actually do a particularly smart job but where the implementation is simple enough that it is at least easy to handle the simple cases they actually care about.

  • I remember how many Windows apps would break if you turned on the large fonts for accessibility; luckily, they'd usually only become hard to use rather than crashing, but it wasn't a certain thing that… – Donal Fellows Jan 18 '13 at 23:12
  • In all the layout managers I have used I think Xcode (since they merged Interface Builder with it) is pretty hard to beat. I don't program for Mac OS X / iOS any more but I do miss the tools. – Cromulent Jan 19 '13 at 13:29
  • 3
    @Cromulent: I think you totally misunderstand. That's a graphical tool for designing GUIs (and an IDE) but a layout manager is a (small) software module for enforcing programmatic rules about how to arrange components within the space provided by a containing component. The most trivial one is to just use a fixed coordinate; that's easy, and wrong because it doesn't respond to size changes. A GUI design tool can be used to write a configuration for a layout manager instance; I've seen that done, but don't know if that's exactly how you do it with Xcode (as front end). – Donal Fellows Jan 21 '13 at 0:38

Consider using MigLayout. It's a comprehensive layout manager that I haven't had any issues with. To get an initial taste of what it's capable of and how easy it is to use, look at the sample application linked to from the home page. Make sure you try resizing the window to see how the user interface is able to adapt to the size of the screen.

The core reason behind the layout managers is to allow your application to scale elegantly to what your user wants or needs. MigLayout makes this easy by providing a huge number of easily tweakable constraints and rules, some of which make it more powerful than HTML+CSS in some areas (though that's finally changing). It also includes a debug mode which shows you exactly what it's trying to do, and has the ability to arrange buttons according to a user's operating system.

A few examples of absolute positioning:

// adds item at 100, 100
panel.add(item, "pos 100, 100");
// adds item 10% + 5 "logical pixels" + 1 inch from left side of parent
// and centers itself 75% of the way down parent component
panel.add(item, "pos (10% + 5lp + 1in) .75al"); 

Details can be found on the tutorial, sample application, and cheat sheet linked to from the main page.


Well, Swing need layout manager because its supposed to be cross platform, and thus will render components of different size on different platform. By using math rule to line elements it make it much more portable.

By the way, if you want good control over the placement while still remaining fluid, you can check GroupLayout. Its not perfect, but it give you a relatively fine control, and I use it very often


I believe the reason this appears so complex is that the behaviour you refer to is implemented by the GridBagLayout manager in standard Java. While GridBagLayout is very expressive, it also includes a large number of parameters in addition to the x,y coords that you might not care about (for example, components spanning multiple cells, anchoring/filling in different ways etc).

While these options can make the documentation a little daunting, you are free to ignore these extra parameters until you need them. Here's a typical usage pattern for me that doesn't sound too different to what you asked for:

setLayout(new GridBagLayout());

// Implicit location = 0,0
// We need to set the weight to be non-zero otherwise everything 
// will clump together in the middle of the frame. Apart from that, 
// we can just provide x,y coords
GridBagConstraints gbc = new GridBagConstraints();

add(myButton, gbc);

// Now add something at 1,0
gbc.gridx = 1;
add(myLabel, gbc);

// Now add something at 2,0
gbc.gridx = 2;
add(myOtherLabel, gbc);

// Now add something at 1,0, it should span the full width and
// it should be anchored to the bottom of the frame
gbc.gridx = 0;
gbc.gridy = 1;
gbc.gridwidth = 3;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.anchor = GridBagConstraints.SOUTH;

add(myOtherComponent, gbc);

As you can see from the item at 0,1, you can have many constraints but you don't need to, just specify the ones you care about.


As already mentioned layoutManagers because screens come in a lot of different sizes.

As already mentioned I would suggest using TableLayout

Most of my screens are fairly simple, I have my own front-end to Swing that allows me to define simple screens like:

pnl.addLine("Record Name", sfRecordName);
pnl.addLine("Description", sfDescription)
pnl.addLine("Record Type", sfRecordType);
pnl.addLine("System",      sfSystem)
pnl.addLine("List",        sfList);

Behind the scene the addLine method uses tableLayout to layout the screen, it also will

  • optionally set the name of the field (to the prompt) which is useful in some Swing Testing tools (e.g. early versions of Marathon)
  • Do language conversion

Have a look around, there are various Swing front-end available


simply specify X and Y coordinates where I want it to be situated

YES, when you resize the window the JLabel and text editor size will not be adjusted based on the screen.it will remain set as same positioning.

You can re adjust the layout with custom layout but its harder to use custom one compared to the layout manager.

If you choose the right layout manager , it will simplify your work lot.

But when you add the layout then the labels size will be adjusted based on the layout .

Not the answer you're looking for? Browse other questions tagged or ask your own question.