3

I'm new to layout managers like Flow, borders, ...
I mostly use setBounds() to set the position of my components.

I read on an article that using setBounds is not an good practice and it's better to use some layout.

Which are the best and most used layouts?
How to position a button using layout managers; instead of doing setbounds(10,10,100,30)?

Alba Mendez
  • 4,432
  • 1
  • 39
  • 55
ramya
  • 175
  • 4
  • 11
  • 2
    I would recommend looking at MeBigFatGuy's link for the Sun tutorials on layouts. After understanding them, though, I would recommend taking a look at [MigLayout](http://www.miglayout.com/). It's quite a bit more intuitive than some of the standard layout managers (in my opinion.) – Chimmy Apr 29 '11 at 05:04
  • 4
    The point of using layout managers is that you don't specifically set the location of size of a component. So you have to stop thinking about absolute locations and sizes and layout your GUI so that is can dynamcially change as the user resizes the frame etc. And you never use just one layout manager. You use different layout managers as required. So you need to learn the basics of each of the standard layout managers. – camickr Apr 29 '11 at 05:06

3 Answers3

5

look here: http://download.oracle.com/javase/tutorial/uiswing/layout/using.html

Basically you should forget about coordinates. Look at your dialogs at a higher level of design. Questions you should ask yourself.

1) Is there a "main" area with smaller surrounding areas in your design. If so use a BorderLayout.

2) Is there equal grid like areas in your design, If so use a GridLayout.

3) If you need a top-down, or left-right layout, consider a BoxLayout

4) If you want to show a complex form, probably use a FormLayout from jgoodies.

But you have to look at things from a high level. There may be subsections in any one top level section of your dialog. If that is the case, then you need to put a JPanel in that section, and then use a sub layout in that JPanel. Use the above questions over again for that panel.

MeBigFatGuy
  • 28,272
  • 7
  • 61
  • 66
  • +1, the tutorial is the best way to answer this. The poster know needs to spend time and do some reading and playing with each of the layout managers to learn their strengths and weaknesses. – camickr Apr 29 '11 at 05:08
  • 1
    +1 for mentioning 'sub layouts'. I do not make too many (non trivial) GUIs that rely on just one layout. – Andrew Thompson Apr 29 '11 at 06:29
2

Besides "standard" Swing layouts (part of the JDK), there are many third-party (mostly open source) LayoutManagers that often are much better than Swing ones.

For a comparison of many LayoutManagers on a real example (with code), check out this link, although a bit old, it still shows the various features and ease of use of predominant LayoutManagers nowadays.

In general, I would advise DesignGridLayout which, although quite powerful, is very easy to use (you don't need a GUI designer to use and it's easy to maintain layout code of existing panels); it just takes one hour to understand it.

Also, MigLayout is viewed as the most flexible one (might be useful if you need very complex layouts), but it takes more time to get used to it, and sometimes you have to use "tricks"to make it work as you want.

jfpoilpret
  • 10,449
  • 2
  • 28
  • 32
2

..which is the best and most used layout (?)

Nested layouts. Use whatever layout works best for different groups of GUI components, then put them in panels inside other panels (with other layouts). See this Nested Layout Example for a demo. of combining layouts.

As to which best individual layouts to use, do the tutorial linked by MBFG to get a feel for what each can achieve, their strengths & weaknesses.

I will commonly use nested combinations of BorderLayout, GridLayout & FlowLayout, with an occasional GridBagLayout.

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 2
    In the past I have used nested layouts a lot, but a major issue with this is that you cannot have consistent sizes and alignment of components across 2 different layouts. That's why now I prefer the "one layout (ie one panel) per window" idiom. But this requires to use powerful Layoutmanagers (eg GridBagLayout, GroupLayout or better, DesignGridLayout and MigLayout). – jfpoilpret Apr 29 '11 at 06:38
  • 2
    same (as @jfpoilpret) here - had been a nesting advocate before the birth of FormLayout, after that only for extremely simple through-away code. You can go with it for a while (as you have shown repeatedly) but there's a point where it takes too much effort to achieve the simplest of alignments. My 2 (Euro) cents, of course – kleopatra Apr 29 '11 at 08:13
  • 1
    +1 There is definitely an art to choosing the right layout manager. I find nested layouts sufficient most of the time, and use the GUI designer's `GroupLayout` for the rest. As a concrete [example](http://stackoverflow.com/questions/3174765/variable-layout-in-swing/3175280#3175280), this common label/text layout can be done with nested layouts, but it's far easier with `MigLayout`, `GroupLayout`, etc. – trashgod Apr 29 '11 at 10:31