Questions tagged [layout-manager]

Layout Managers are a collection of standard Java based layout managers for AWT & Swing components. The managers handle the logic of how to size, position & align Components within a Container, and set the orientation of the container so that it is appropriate for the locale in which the program is running.

LayoutManager is the Java interface for classes that know how to layout the contents of Container. The typical containers include JPanel for Swing and Panel for AWT but many others (like Window) are possible.

The layout manager lays out container components following rules that are different for each implementation. For instance, GridLayout arranges components in a table-like way while BorderLayout places one into center and up to four others at each edge.

While layout managers often call getPreferredSize on the component to know how much space it would need, the size hints are also regularly ignored. E.G. in GridLayout all components become the size of the largest width and height, in BorderLayout the CENTER component will be stretched to the available space, the top/bottom will be stretched in width and the line start/end will be stretched in height).

Layouts are typically combined in order to achieve complex GUIs. E.G. as seen in the Nested Layout Example.

If the layout manager of some particular component is set to null, components can be positioned in arbitrary way by setting they bounds manually. This causes many problems, & the first step in fixing those problems is typically to 'set a layout'.

Existing Layout Managers

AWT

  • BorderLayout. Provides a CENTER & 4 borders in which to place components.
  • CardLayout good for swapping many components or views in a single parent container.
  • FlowLayout easiest to use, puts components one after another, left-right, top-to-bottom. Typically only suited to single rows of components.
  • GridBagLayout can do some simple tasks easily (like 'center at preferred size') yet also allows complex layouts.
  • GridLayout provides a WxH grid of component spaces, each the same size.

Swing

  • BoxLayout provides a 'box' like construct for adding components in a row or column.
  • GroupLayout is powerful, allowing alignment across components and component groups, but is generally considered to be a layout for use by an IDE.
  • OverlayLayout allows components to be stacked.
  • ScrollPaneLayout Used internally by other Swing components.
  • SpringLayout is a way of layoun out components with white space allowing alignment along component edges.
  • ViewportLayout Used internally by other Swing components.

3rd party

Relevant tutorials:

2999 questions
15
votes
1 answer

Use of overriding getPreferredSize() instead of using setPreferredSize() for fixed size Components

I read some posts here and I started why some people do @Override public Dimension getPreferredSize() { return new Dimension(500, 500); } instead of setPreferredSize(new Dimension(500, 500)); Isn't the second one better because it creates only…
mmaag
  • 1,534
  • 2
  • 18
  • 24
15
votes
2 answers

What's so special about CardLayout vs manual adding/removal of JPanels?

There have been many times on StackOverflow where a user asks a question like this... I have a main JPanel that contains a child JPanel. When the user clicks a button, the child JPanel should change to a different JPanel. How can I achieve…
wattostudios
  • 8,666
  • 13
  • 43
  • 57
14
votes
1 answer

GroupLayout can only be used with one container at a time

Not sure why this error is coming up. I am using GroupLayout because I want it to do the spacing for me and will be adding more panels to the frame in the future. Below is the stack trace. Exception in thread "main"…
marcopolo
  • 1,963
  • 4
  • 18
  • 31
13
votes
2 answers

Why doesn't setLocation() move my label?

I have the following code where I try to place a JLabel in a custom location on a JFrame. public class GUI extends JFrame { /** * * @param args */ public static void main(String args[]) { new GUI(); } …
rob
  • 141
  • 1
  • 2
  • 5
13
votes
5 answers

GridBagLayout: equally distributed cells

Is it possible to completely emulate the behavior of a GridLayout with the GridBagLayout manager? Basically, I have a 8x8 grid in which each cell should have the same width and height. The GridLayout automatically did this. But I want to add another…
poke
  • 369,085
  • 72
  • 557
  • 602
13
votes
1 answer

How to remove the padding between in the JPanel still using a flow layout?

Here's the portion of my java application GUI that I have a question about. What this GUI consists is a blue JPanel(container) with default FlowLayout as LayoutManager that contains a Box which contains two JPanels(to remove the horizontal spacing…
committedandroider
  • 8,711
  • 14
  • 71
  • 126
13
votes
1 answer

Create a Chess board with JPanel

I have a simple Chess board in a JPanel with GridLayout(8,8) as layout manager. I am trying to add panels for the fields' column name and row number. Right now I've created another panel with BorderLayout as layout manager, and in this panel I add…
Jamgreen
  • 10,329
  • 29
  • 113
  • 224
13
votes
5 answers

How to make JLabels start on the next line

JPanel pMeasure = new JPanel(); .... JLabel economy = new JLabel("Economy"); JLabel regularity = new JLabel("Regularity"); pMeasure.add(economy); pMeasure.add(regularity); ... When I run the code above I get this output: Economy Regularity How can…
Jessy
  • 15,321
  • 31
  • 83
  • 100
12
votes
6 answers

Java Swing JFrame Layout

I just wrote a simple code where I want a textfield and a button to appear on the main frame, but after running all I see is the textfield. If I write the code of the button after the textfield then only the button is displayed. Any idea why? …
DanMatlin
  • 1,212
  • 7
  • 19
  • 37
12
votes
2 answers

java JPanel How to fixed sizes

I want to have a resizable panel, that always has the top green panel of a fixed depth. i.e. all changes in height should effect the yellow panel only. My code below is almost OK, except the green panel varies in size a little. How do I do this? …
ManInMoon
  • 6,795
  • 15
  • 70
  • 133
12
votes
2 answers

How to use margins and paddings with Java GridLayout

How can I keep a JLabel from displaying flush against the side of the frame? I have the same problem when using GridLayout or BoxLayout. Here's an example where this happens: JPanel content = new JPanel(); content.setLayout(new BoxLayout(content,…
Alison
  • 5,630
  • 4
  • 18
  • 26
11
votes
2 answers

JavaScript layout manager?

I am looking for a JavaScript layout manager that has support for flexible layouts: nesting, resizing, splitters, collapsible panels etc. I've seen ExtJS and Dojo but I'm wondering if there are any lightweight variants that excel at this ... Thanks!
dani
  • 4,880
  • 8
  • 55
  • 95
11
votes
5 answers

Are there no built in C# GUI Layouts?

I'm used to the GUI frameworks in Java as well as the QT GUI framework, and I'm used to the various layout managers. It doesn't seem that C# has any layout managers built in, or am I missing something? 2 Year Later Edit I just want to point out to…
Anthony
  • 9,451
  • 9
  • 45
  • 72
11
votes
3 answers

How to right-justify icon in a JLabel?

For a JLabel with icon, if you setHorizontalTextPosition(SwingConstants.LEADING), the icon is painted right after text, no matter how wide the label is. This is particularly bad for a list, as the icons would be all over the place depending on how…
Geoffrey Zheng
  • 6,562
  • 2
  • 38
  • 47
11
votes
2 answers

Best Practice Guide: Swing

Does anybody know Swing related GUI guidelines - specifically on how to design Swing apps and which components I should use? I'm not looking for an official standard, but pragmatic tips I can use to set a good standard for my projects. I haven't…
wishi
  • 7,188
  • 17
  • 64
  • 103