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
506
votes
9 answers

Should I avoid the use of set(Preferred|Maximum|Minimum) size methods in Java Swing?

Several times I've been criticized for having suggested the use of the following methods: setPreferredSize() setMinimumSize() setMaximumSize() on Swing components. I don't see any alternatives to their use when I want to define proportions between…
Heisenbug
  • 38,762
  • 28
  • 132
  • 190
245
votes
11 answers

How to add JTable in JPanel with null layout?

I want to add JTable into JPanel whose layout is null. JPanel contains other components. I have to add JTable at proper position.
Sagar
  • 2,475
  • 2
  • 15
  • 3
173
votes
6 answers

Providing white space in a Swing GUI

A GUI with no white space appears 'crowded'. How can I provide white space without resorting to explicitly setting the position or size of…
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
88
votes
5 answers

Swing GUI listeners without AWT

I am a starting Java developer, learning just from internet tutorials. I am learning full-screen GUI applications. I was told yesterday that I shouldn't use AWT in my programs because it is outdated. I already know about light and heavyweight…
Hidde
  • 11,493
  • 8
  • 43
  • 68
63
votes
9 answers

How to position the form in the center screen?

I'm a .Net developer but somehow I was task to create a simple application in java for some extra reason. I was able to create that application but my problem is how can i center the form in the screen when the application is launched? Here is my…
John Woo
  • 258,903
  • 69
  • 498
  • 492
49
votes
3 answers

Span multiple columns with RecyclerView

So what I am trying to go for is having a staggered layout but the first item in the list needs to span two columns. The first two rows are also a fixed height. I have everything working except the first item spanning two columns. I am using the…
DDukesterman
  • 1,391
  • 5
  • 24
  • 42
47
votes
5 answers

LinearLayoutManager setReverseLayout() == true but items stack from bottom

This seems like it would be an easy solution, but it seems that setting private RecyclerView mRecyclerView; private RecyclerView.Adapter mAdapter; private LinearLayoutManager mLayoutManager; .... // More code mRecyclerView = (RecyclerView)…
AndyRoid
  • 5,062
  • 8
  • 38
  • 73
47
votes
14 answers

How do I change JPanel inside a JFrame on the fly?

To put it simple, there's a simple java swing app that consists of JFrame with some components in it. One of the components is a JPanel that is meant to be replaced by another JPanel on user action. So, what's the correct way of doing such a thing?…
yanchenko
  • 56,576
  • 33
  • 147
  • 165
44
votes
14 answers

Which Swing layout(s) do you recommend?

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…
Carl Smotricz
  • 66,391
  • 18
  • 125
  • 167
32
votes
7 answers

Centering a JLabel in a JPanel

I have a panel derived from JPanel. I have a custom control derived from JLabel. I am attempting to center this custom JLabel on my panel. The only way I know to do this that will work is to use the a null layout (setLayout(null)) and then calculate…
Shackleford
  • 379
  • 1
  • 4
  • 4
31
votes
5 answers

How to use MigLayout?

I read some information about Java layout managers and the recommendation was MigLayout. My problem is, that I didn't found any tutorial or easy step-by-step documentation. So I want to know how this all works, but I don't know how.
guerda
  • 23,388
  • 27
  • 97
  • 146
28
votes
2 answers

Making a robust, resizable Swing Chess GUI

How would I go about making this resizable Chess GUI? Our company has been tasked with making a Chess game. It needs to work on Windows, OS X and Linux/Unix machines, and we have chosen Java to achieve this, while maintaining a common code-base…
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
27
votes
5 answers

jQuery Layout Manager

Has anyone had any experience with a jQuery-based layout manager? I know of only one (JX) but it's somewhat bloated for my needs. Any thoughts?
Karim
  • 18,347
  • 13
  • 61
  • 70
26
votes
1 answer

How to use scrollVerticallyBy() with Android RecyclerView and LayoutManager?

I would like to scroll by a certain amount of pixels in y direction in my recyclerview. I know that there are methods like scrollToPosition() or scrollToPositionWithOffset(). However, those don't really fit my needs. I saw that LayoutManager…
user2426316
  • 7,131
  • 20
  • 52
  • 83
26
votes
4 answers

How can I control the width of JTextFields in Java Swing?

I am trying to have several JTextFields on a single row, but I don't want them to have the same width. How can I control the width and make some of them wider than others? I want that they together take up 100% of the total width, so it would be…
Jonas
  • 121,568
  • 97
  • 310
  • 388
1
2 3
99 100