7
Layouts
In this chapter:
- The LayoutManager Interface
- FlowLayout
- BorderLayout
- GridLayout
- CardLayout
- GridBagLayout
- GridBagConstraints
- Combining Layouts
- Disabling the LayoutManager
- Designing Your Own LayoutManager
- The sun.awt Layout Collection
- Other Layouts Available on the Net
This chapter expands upon the idea of a layout manager, which was mentioned briefly in the previous chapter. Every container has a LayoutManager that is responsible for positioning the component objects within it, regardless of the platform or the screen size. Layout managers eliminate the need to compute component placement on your own, which would be a losing proposition since the size required for any component depends on the platform on which it is displayed. Even for a simple layout, the code required to discover component sizes and compute absolute positions could be hundreds of lines, particularly if you concern yourself with what happens when the user resizes a window. A layout manager takes care of this for you. It asks each component in the layout how much space it requires, then arranges the components on the screen as best it can, based on the component sizes on the platform in use and the space available, resizing the components as needed.
To find out how much space a component needs, a layout manager calls the component's getMinimumSize() and getPreferredSize() methods. (Java 1.1 also has a getMaximumSize() method; the existing layout managers don't take advantage of it.) These methods report ...
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.
Read now
Unlock full access