17.6. Container Layout Managers
As I said earlier, an object called a layout manager determines the way that components are arranged in a container. All containers will have a default layout manager, but you can choose a different layout manager when necessary. Many layout manager classes are provided in the java.awt and javax.swing packages, so I'll introduce those that you are most likely to need. It is also possible to create your own layout manager classes, but creating layout managers is beyond the scope of this book. The layout manager for a container determines the position and size of all the components in the container, so you should not generally change the size and position of such components yourself; just let the layout manager take care of it.
Since the classes that define layout managers all implement the LayoutManager interface, you can use a variable of type LayoutManager to store any of them if necessary. I'll introduce six layout manager classes in a little more detail. The names of these classes and the basic arrangements that they provide are as follows:
Layout Manager | Description |
---|---|
FlowLayout | Places components in successive rows in a container, fitting as many on each row as possible and starting on the next row as soon as a row is full. This works in much the same way as your text processor places words on a line. Its primary use is for arranging buttons, although you can use it with other components. It is the default layout manager for JPanel objects. |
BorderLayout ... |
Get Ivor Horton's Beginning Java™ 2, JDK™ 5th Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.