Chapter 8. Swing Containers

In this chapter, we’ll take a look at a number of components Swing provides for grouping other components together. In AWT, such components extended java.awt.Container and included Panel, Window, Frame, and Dialog. With Swing, you get a whole new set of options, providing greater flexibility and power.

A Simple Container

Not everything in this chapter is more complex than its AWT counterpart. As proof of this claim, we’ll start the chapter with a look at the JPanel class, a very simple Swing container.

The JPanel Class

JPanel is an extension of JComponent (which, remember, extends java.awt.Container) used for grouping together other components. It gets most of its implementation from its superclasses. Typically, using JPanel amounts to instantiating it, setting a layout manager (this can be set in the constructor and defaults to a FlowLayout), and adding components to it using the add() methods inherited from Container.

Properties

JPanel does not define any new properties. Table 8.1 shows the default values that differ from those provided by JComponent.

Table 8-1. JPanel Properties

Property

Data Type

get

is

set

bound

Default Value

See also properties from the JComponent class (xref linkend="SWING-CH-3-TABLE-10"/>).       

UIClassID

String

•

   

"PanelUI"

accessibleContext*

AccessibleContext

•

   

JPanel.AccessibleJPanel()

doubleBuffered*

true

 

•

•

 

true

layout*

LayoutManager

•

 

•

Get Java Swing now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.