JFrame : Java Glossary

home page J words local find full screen, hide local find menu Google search web for more information on this topic jump to foot of page translate this page with Babelfish punctuation 0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z (all) ©1996-2010 2008-02-14 Roedy Green, Canadian Mind Products
JFrame
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

Tips

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 co-ordinates 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. Sun 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 Paint Shop 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 setLocaton to position it, then a validate, and finally a setVisible ( true ).

You might to 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

Sun’s Javadoc on JFrame class : available:

CMP homejump to top You can get the freshest copy of this page from: or possibly from your local J: drive (Java virtual drive/mindprod.com website mirror)
http://mindprod.com/jgloss/jframe.html J:\mindprod\jgloss\jframe.html
CMP logofeedback Please email your feedback for publication, errors, omissions, typos, formatting errors, ambiguities, unclear wording, broken/redirected link reports, suggestions to improve this page or comments to Roedy Green : feedback email
mindprod.com IP:[65.110.21.43]
view BlogYour face IP:[38.107.191.111]
You are visitor number 59,056.