Java Reference
In-Depth Information
CHAPTER 11
■ ■ ■
Advanced Swing Containers
C hapter 10 explored the layout managers available within AWT and Swing. In this chapter,
you'll look at some of the containers that rely on these layout managers, as well as some others
that work without a layout manager.
Starting with the Box class, you'll discover the best way to use the BoxLayout manager to
create a single row or column of components. Next, you'll learn about the JSplitPane container,
which is a bit like a specialized Box with just two components inside. The JSplitPane provides
a splitter bar that acts as a divider users can drag to resize the components to suit their needs.
Then you'll explore the JTabbedPane container, which works something like a container
whose layout manager is a CardLayout , except with tabs built into the container that allow you
to move from card to card. You'll be able to create multiple-screen, property-sheet dialog
boxes for user input with JTabbedPane .
The last two advanced Swing containers covered are the JScrollPane and JViewport . Both
of these components offer the ability to display a section of a large component within a limited
amount of screen real estate. The JScrollPane adds scrollbars to a display area so that you can
move around a large component that sits within a small area. In fact, the JScrollPane uses the
JViewport to “clip away” the part of the larger component that shouldn't be seen.
So, let's get started and look at the first container, the Box class.
Box Class
As a subclass of JComponent , the Box class is a special Java Container for creating a single row or
column of components with the help of the BoxLayout manager. The Box container works like a
JPanel (or Panel ), but has a different default layout manager, BoxLayout . Using BoxLayout can
be a little cumbersome without a Box , which simplifies working with BoxLayout . You can associate
the BoxLayout manager with a container in just three steps: manually creating the container,
creating the layout manager, and associating the manager with the container. When you create
an instance of Box , you perform these three steps at once. In addition, you can use an inner
class of Box called Box.Filler to better position components within the container.
377
Search WWH ::




Custom Search