JFrame : Java Glossary

go to 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 2008-02-14 by Roedy Green ©1996-2009 Canadian Mind Products
index page for letter ⇒ 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)
JFrame
A Window with controls on it such as optional resizing buttons, an icon, a menubar and a title.

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.

You don’t use jFrame.add( child ); you use jFrame.getContentPane().add( child ). This is especially infuriating since the compiler cannot catch the error. Similarly instead of jFrame.setLayout( layoutManager ) you use jFrame.getContentPane().setLayout( layoutManager ). Ditto for jFrame.getContentPane().setBackground( Color ) and jFrame.remove( child ). JDK 1.5+ is more forgiving and your code still works if you forget to use contentPane.

You continue to use jFrame.setSize, jFrame.validate and jFrame.setVisible.

You can get a decorated frame with JFrame.setDefaultLookAndFeelDecorated( true ); The unusual thing is this is a static method on JFrame, not a method for JFrame objects.

You can change the coffee cup icon for the Jframe with setIconImage.

The AWT equivalent of JFrame is called Frame.

You nearly always set your own layout manager since by default JFrame comes with the lame BorderLayout.

JFrame is a heavyweight component, more like an AWT component than a Swing one. It does not have a paintComponent, but an AWT-style paint. Don’t paint directly on it. Paint on a JPanel instead and insert that in the JFrame.

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. One is to extend JFrame and the other is to create a class with a reference to a JFrame. The second method gives a better separation of logic. The first is less verbose.

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

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 the JFrame class : available:

CMP homejump to top
CMP logo
feedback Please email your feedback for publication, errors, omissions, broken/redirected link reports
and suggestions to improve this page to Roedy Green : feedback email
made with CSS
HTML Checked!
ICRA ratings logo
mindprod.com IP:[65.110.21.43]
Your face IP:[38.103.63.62] You need Adobe flash to see this public service ad.
You are visitor number 48,610.
You can get a fresh 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