Week 9 - Lab Notes - Layout Managers and Widgets

Recommended Reading

Be sure to read the section of the tutorial entitled Laying out Components

Also, be sure to look over the java.awt API documentation. It gives a big run-down of all the Layout Managers and Widgets.

Layout Managers

Java uses an approach to laying out widgets in an application that is similar to the approach taken in HTML. As we have learned, HTML was designed to be viewed on a variety of displays and is therefore deliberately imprecise. Similarly, Java uses the notion of Layout Managers to position widgets on the screen in general areas, rather than in hard-and-fax X,Y coordinates. This may seem a little awkward at first, it certainly is a new way of thinking about the appearance of your application, but it grows one you.

You might want to read the section of the tutorial entitled Layout Management to understand the theory behind Layout Managers.

Incidentally, you can position your components with hard-and-fast X,Y coordinates if you really want to. The section Doing Without a Layout Manager (Absolute Positioning) describes how it is done. I strongly encourage you to use a Layout Manager, however.

Types of Layout Managers

For a listing of all the different kinds of Layout Managers, including a description of each and a picture of how they look, please read the Using Layout Managers section of the tutorial.

Here are my personal favorites:

If you do not specify a particular Layout Manager, the default is FlowLayout.


It is doubtful that you would ever want to stick just one widget into the cell provided by a Layout Manager. Java provides a Panel object that you can insert into a cell of a Layout Manager. A Panel can have its own layout manager and hold it's own controls. A panel would therefore be useful for making a toolbar, a status bar, or a row of buttons in an application.

The Graphics Example Applet that I did awhile ago uses a panel to create a column of buttons down the left-hand side of the applet.


Look at the Basic Controls page to get a visual overview of what the basic containers and widgets are and what they look like. All of the widgets pictured have descriptive text hyperlinked to other sections in the tutorial. Said sections contain lengthy explanations of the various widgets, longer descriptions of their API, and copious sample code that shows the widgets in action. I will not duplicate any of that information in these notes so please use that page as a reference in your studies.


3/8/99 - Initial Revision