Recipe 14.1 Displaying GUI Components


Problem

You want to create some GUI components and have them appear in a window.

Solution

Create a JFrame and add the components to its ContentPane.

Discussion

The older Abstract Windowing Toolkit (AWT) had a simple Frame component for making main windows; this allowed you to add components directly to it. "Good" programs usually created a panel to fit inside and populate the frame. But some less-educated heathens, and those in a hurry, often added components directly to the frame. The Swing JFrame is more complex it comes with not one but two containers already constructed inside it. The ContentPane is the main container; you should normally use it as your JFrame's main container. The GlassPane has a clear background and sits over the top of the ContentPane; its primary use is in temporarily painting something over the top of the main ContentPane. Because of this, you need to use the JFrame's getContentPane( ) method:

import java.awt.*; import javax.swing.*; public class ContentPane extends JFrame {     public ContentPane( ) {         Container cp = getContentPane( );         // now add Components to "cp"...     } }

You can add any number of components (including containers) into this existing container, using the ContentPane add( ) method:

import java.awt.*; import java.awt.event.*; import javax.swing.*; /** Just a Frame  * @version $Id: ch14.xml,v 1.6 2004/05/07 15:20:56 ian Exp $  */ public class JFrameDemo extends JFrame {     JButton quitButton;     /** Construct the object including its GUI */     public JFrameDemo( ) {         super("JFrameDemo");         Container cp = getContentPane( );         cp.add(quitButton = new JButton("Exit"));         // Set up so that "Close" will exit the program,          // not just close the JFrame.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);         // This "action handler" will be explained later in the chapter.         quitButton.addActionListener(new ActionListener( ) {             public void actionPerformed(ActionEvent e) {                 setVisible(false);                 dispose( );                 System.exit(0);             }         });                      pack( );     }     public static void main(String[] args) {         new JFrameDemo( ).setVisible(true);     } }

This code compiles fine. But when we try to run it, of course, there is no main method. We need to create one, either inside the JFrameDemo class or on its own:

public class JFrameDemoMain {     // We need a main program to instantiate and show.     public static void main(String[] args) {         new JFrameDemo( ).setVisible(true);     } }

Now we can run it and have it display. But it has two obvious problems: it starts off tiny (on Windows) or huge (on X Windows). And, when we do resize it, only the buttons show, and it always takes up the full size of the window. To solve these problems, we need to discuss layout management, to which we now turn our attention.

A less obvious problem has to do with thread safety (see Chapter 24). The basic idea is that the first component that gets created starts a Thread of control running, and both this thread and your main thread can be doing things to the GUI at the same time. The solution is to do the setVisible(true) on the window system's event thread, using the static EventQueue.invokeLater( ) method. The code to start the GUI in a thread-safe way is only a few lines longer. This code uses an "anonymous inner class"; see Recipe 9.6 for details on this technique:

public class JFrameDemoSafe {         // We need a main program to instantiate and show.         public static void main(String[] args) {                // Create the GUI (variable is final because used by inner class).                 final JFrame demo = new JFrameDemo( );                // Create a Runnable to set the main visible, and get Swing to invoke.                 EventQueue.invokeLater(new Runnable( ) {                         public void run( ) {                                 demo.setVisible(true);                         }                 });         } }

Most books and articles on Swing GUIs do not mention Swing's thread-safety issues, but you can read about it on Sun's web site, at http://java.sun.com/developer/JDCTechTips/2003/tt1208.html. We will omit this code for brevity from the simple demos herein, but production code should use it.



Java Cookbook
Java Cookbook, Second Edition
ISBN: 0596007019
EAN: 2147483647
Year: 2003
Pages: 409
Authors: Ian F Darwin

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net