The JFrame Class

The most common Swing container for Java applications is the JFrame class. Like java.awt.Frame, JFrame provides a top-level window with a title, border, and other platform-specific adornments (e.g., minimize, maximize, and close buttons). Because it uses a JRootPane as its only child, working with a JFrame is slightly different than working with an AWT Frame. An empty JFrame is shown in Figure 8-9.

Empty JFrame instances on Unix, Mac, and Windows platforms

Figure 8-9. Empty JFrame instances on Unix, Mac, and Windows platforms

The primary difference is that calls to add( ) must be replaced with calls to getContentPane( ).add( ). In fact, the addImpl( ) method is implemented so that a call made directly to add( ) throws an Error. (The error message tells you not to call add( ) directly.)

Properties

JFrame defines the properties shown in Table 8-8. The accessibleContext property is as expected. ContentPane , glassPane, layeredPane, and JMenuBar are really properties of JRootPane (described earlier in the chapter). JFrame provides direct access to these panes, as required by the RootPaneContainer interface.

Table 8-8. JFrame properties

Property

Data type

get

is

set

Default value

accessibleContext o

AccessibleContext

·

  

JFrame.Accessible-JFrame( )

background o

Color

·

 

·

UIManager.getColor ("control")

contentPane o

Container

·

 

·

From rootPane

defaultCloseOperation

int

·

 

·

HIDE_ON_CLOSE

glassPane o

Component

·

 

·

From rootPane ...

Get Java Swing, 2nd Edition now with O’Reilly online learning.

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