JFrame : Java Glossary


A Window with controls on it such as optional resizing buttons, an icon, a menubar and a title.
Tips Caller/Callee Responsibility
Co-ordinate System JFrame Closing
JFrame State Learning More
IsA vs HasA Links
Debugging Harness


Co-ordinate System

When you specify the Jframe size, make it 22 pixels wider and 32 pixels taller than you need for the contents to allow room for the top Jframe bar. To add to the confusion, the origin is in the top corner under the Jframe bar. So you must add 32 to all your y coordinates inside a Jframe. This is one more reason to use layouts and avoid absolute positioning.

There are four way Jframe sizes could have been measured: all combinations of including or excluding the title bar and including or excluding the border. Oracle chose to include both. The easiest way to calculate a Jframe size is to manually adjust the Jframe until it looks right, then take a snapshot of it with PaintShop Pro, including the title bar and borders and use PSP’s size as your jFrame.setSize to set the initial size.

Jframe State

This works the same as a Frame.
// turn off the entire frame bar, including title, close, minimize, resize...
frame.setUndecorated( true );

// turn off just resizing controls
frame.setResizable( false );
// You can control the current state of the Frame,
// but you cannot disable the corresponding user controls.
// To control the widgets, use a JInternalFrame.

frame.setExtendedState( NORMAL );
frame.setExtendedState( ICONIFIED );
frame.setExtendedState( MAXIMIZED_HORIZ );
frame.setExtendedState( MAXIMIZED_VERT );
frame.setExtendedState( MAXIMIZED_BOTH );

Beware. getExtendedState in a componentResized event handler gives you the old state. You could use SwingUtilities. invokeLater or EventQueue. invokeLater to probe the new state.

IsA vs HasA

There are two techniques for creating a frame full of components.

  1. Extend Jframe with private variables, getters and setters.
  2. Create a class with private variables, getters and setters that contains a reference to a standard Jframe object.

Advantages of Extension

Advantages of Composition

Debugging Harness

Here is a typical debugging harness you would append to every Jframe you write. Note how all initialisation is done on the Swing event thread via SwingUtilities.

Caller/Callee Responsibility

Normally the caller who instantiates the Jframe does a setSize to determine its size, then a setLocation to position it, then a validate and finally a setVisible ( true ).

You might do a setSize and setLocation in the constructor, but definitely not a setVisible. setTitle may be done by either.

Jframe Closing

Jframes are unusually tricky to close.

Learning More

Oracle’s Javadoc on JFrame class : available:

This page is posted
on the web at:


Optional Replicator mirror
of mindprod.com
on local hard disk J:

Canadian Mind Products
Please the feedback from other visitors, or your own feedback about the site.
Contact Roedy. Please feel free to link to this page without explicit permission.

Your face IP:[]
You are visitor number