Skip to Content
Learning Java, 4th Edition
book

Learning Java, 4th Edition

by Patrick Niemeyer, Daniel Leuck
June 2013
Beginner
1007 pages
33h 32m
English
O'Reilly Media, Inc.
Content preview from Learning Java, 4th Edition

Chapter 19. Layout Managers

A layout manager arranges the child components of a container, as shown in Figure 19-1. It positions and sets the size of components within the container’s display area according to a particular layout scheme. The layout manager’s job is to fit the components into the available area while maintaining some spatial relationships among them. AWT and Swing come with several standard layout managers that will collectively handle most situations; you can also make your own layout managers if you have special requirements.

A layout manager at work

Figure 19-1. A layout manager at work

Every container has a default layout manager. When you make a new container, it comes with a LayoutManager object of the appropriate type. You can install a new layout manager at any time by using the setLayout() method. For example, we can set the layout manager of a Swing container to a type called BorderLayout like so:

    myContainer.setLayout( new BorderLayout() );

Notice that although we have created a BorderLayout, we haven’t bothered to save a reference to it. This is typical; after installing a layout manager, it usually does its work behind the scenes by interacting with the container. You rarely call the layout manager’s methods directly, so you don’t usually need a reference (a notable exception is CardLayout). However, you do need to know what the layout manager is going to do with your components ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Learning Java, 6th Edition

Learning Java, 6th Edition

Marc Loy, Patrick Niemeyer, Daniel Leuck
Learning Java, 5th Edition

Learning Java, 5th Edition

Marc Loy, Patrick Niemeyer, Daniel Leuck
Head First Java, 3rd Edition

Head First Java, 3rd Edition

Kathy Sierra, Bert Bates, Trisha Gee
Java in a Nutshell, 7th Edition

Java in a Nutshell, 7th Edition

Benjamin J. Evans, David Flanagan

Publisher Resources

ISBN: 9781449372477Errata PageSupplemental Content