Download the JDK
Search the Tutorials
Hide the TOC
Trail: Creating a GUI with JFC/Swing
Lesson: Laying Out Components Within a Container
Laying Out Components Within a Container
A Visual Guide to Layout Managers
Using Layout Managers
How Layout Management Works
How to Use Various Layout Managers
How to Use BorderLayout
How to Use BoxLayout
How to Use CardLayout
How to Use FlowLayout
How to Use GridBagLayout
How to Use GridLayout
How to Use GroupLayout
A GroupLayout Example
How to Use SpringLayout
Creating a Custom Layout Manager
Doing Without a Layout Manager (Absolute Positioning)
Solving Common Layout Problems
Home Page > Creating a GUI with JFC/Swing > Laying Out Components Within a Container
« Previous • Trail • Next »
Solving Common Layout Problems

> Problem: How do I specify a component's exact size?
> - First, make sure that you really need to set the component's exact size. Each Swing component has a different preferred size, depending on the font it uses and the look and feel. For this reason, it often doesn't make sense to specify a Swing component's exact size.

> - If the component isn't controlled by a layout manager, you can set its size by invoking the `setSize` or `setBounds` method on it. Otherwise, you need to provide size hints and then make sure you're using a layout manager that respects the size hints.

> - If you extend a Swing component class, you can give size hints by overriding the component's `getMinimumSize`, `getPreferredSize`, and `getMaximumSize` methods. What's nice about this approach is that each `getXxxxSize` method can get the component's default size hints by invoking `super.getXxxxSize()`. Then it can adjust the size, if necessary, before returning it.

> - Another way to give size hints is to invoke the component's `setMinimumSize`, `setPreferredSize`, and `setMaximumSize` methods.

> > Note: No matter how you specify your component's size, be sure that your component's container uses a layout manager that respects the requested size of the component. The `FlowLayout` and `GridBagLayout` managers use the component's preferred size (the latter depending on the constraints that you set), but `BorderLayout` and `GridLayout` usually don't. The `BoxLayout` manager generally uses a component's preferred size (although components can be larger), and is one of the few layout managers that respects the component's maximum size.

> > If you specify new size hints for a component that's already visible, you then need to invoke the `revalidate` method on it, to make sure that its containment hierarchy is laid out again. Then invoke the `repaint` method.

> Problem: My custom component is being sized too small.

> - Does the component implement the `getPreferredSize` and `getMinimumSize` methods? If so, do they return the right values?
> - Are you using a layout manager that can use as much space as is available? See Tips on Choosing a Layout Manager for some tips on choosing a layout manager and specifying that it use the maximum available space for a particular component.

> If you don't see your problem in this list, see Solving Common Component Problems.

« Previous • Trail • Next »
Previous page: Doing Without a Layout Manager (Absolute Positioning)
Next page: Questions and Exercises: Laying Out Components within a Container