O'Reilly logo

Java Swing, 2nd Edition by Brian Cole, James Elliott, Marc Loy, Robert Eckstein, Dave Wood

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required