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 | Default value |
|---|---|---|---|---|---|
| AccessibleContext | · | JPanel.AccessibleJPanel( ) | ||
doubleBufferedo | boolean | · | · | true | |
layouto | LayoutManager | · | · | FlowLayout( ) | |
opaqueo, b | boolean | · | · | true | |
UI1.4 | PaneUI | · | · | From L&F | |
UIClassID | String | · | “PanelUI” | ||
1.4since 1.4, bbound, ooverridden See also
properties from the |
The doubleBuffered
and opaque
properties default to true, while the layoutManager defaults to a new FlowLayout.
Constructors
- public JPanel( )
Create a new panel with a
FlowLayoutand double buffering.- public JPanel(boolean isDoubleBuffered)
Create a new panel with a
FlowLayout. Double buffering is enabled ifisDoubleBufferedistrue.- public JPanel(LayoutManager layout) ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access