Several times now we've glossed over the supposedly simple processes behind arranging your user interface on the screen. Now it's time to own up: Creating a user interface can be simple; it can also be as complex as you like. This week we're going to spend a little time explaining some of the more complicated possibilities for controlling the layout of your applet. Controlling Applet Layout
The Layout Manager
All applets inherit from a class calledjava.awt.Panel; simply, this means that all applets possess basic user interface facilities. Panels are spaces where interface "components"--buttons, text areas, and even other panels--can be attached. Panels are part of the event chain, meaning that the windowing system passes events (e.g., mouse, keyboard, or scope related) through the Panel when they are received.Finally, the Panel, as a type of
Container, has something called a layout manager associated with it. The layout manager has only one specific job, which is to determine the location and size of components within a Container.In other words, practically speaking, your layout managers determine how and where the objects in your interface are placed. You may remember that we've mentioned the default layout manager for applets (for Panels in general) is the
FlowLayoutmanager. As layout managers go, it's extraordinarily simple:
Basically, components are placed in a straight horizontal line as you
add()them.We should say from the outset that it's not necessary for your applets to use an arbitrary, predefined layout manager. You can write your own; it's done by implementing something called the "LayoutManager interface." Or simpler yet, you can choose not to use a layout manager at all. In that case, you would need to specify the absolute (x and y coordinate) locations for every component you want to place. This is tedious, not to mention unwise, under many circumstances. Since interface components such as buttons and menus are created by the windowing environment of whatever machine your applet may be running on, in whatever size with whatever font, the set of positions that may look good on your Macintosh may look awful on your XWindows display.
Fortunately, the set of premade layout managers available to us in the API is passable. You can select among them by instantiating a new layout object of the type you desire and passing it as a parameter to the
setLayoutfunction: setLayout(new CardLayout());setLayout, as we hinted at earlier, is a method on theContainerclass and, by extension (or really by inheritance), a method of your Applet.Basically, there are five premade layout managers to choose from (including FlowLayout). Unlike the FlowLayout, which needs no extra instructions, the others may require you to specify additional information or take an extra step when adding components.
BorderLayoutis, possibly, the simplest of all. It allows for only five buttons, positioned at the top, bottom, left, right, and center of your Container.GridLayoutis equally mundane; you can specify a number of rows and/or columns, and the buttons youadd()fall into the panel in an orderly grid.Perhaps more interesting is
CardLayout. This layout manager is designed for keeping track of, and flipping between, multiple Containers. The effect can be that, within your "main" window, your interface flips between different "cards," each containing different elements.The most intricate of all the layout managers is
GridBagLayout. It allows you to configure your layout with respect to preferred sizes and spatial relationships of its components. It can be very complex, but it rounds out the collection of layout managers nicely. We can easily spend an entire column talking about its efficient use, which we will in the near future.The Sun explanation that goes along with these classes, which we've linked to throughout, is precise and thorough. We recommend you check it out. For more than a cursory component-based interface, you'll need to understand layouts.
Check us out next week for more details on how they work.