Anatomy of a Swing App: Stuff You Typically Need to Do in Swing

     

Starting the App: InvokeLater()

First, the main method calls a static method from the javax.swing.SwingUtilities class called invokeLater() . This method is suited for starting the application, as its purpose is to update the GUI asynchronously on the event dispatching thread.

You can call this method from any thread if you want the event dispatching thread to run some code. If used in this way, it would be invoked like so:

 

 Runnable updateGUI = new Runnable() {     public void run() {        new MyGUI();     } }; SwingUtilities.invokeLater(updateGUI); 

Here, we just create a new object of a type called MyGUI, which probably extends JFrame or holds a JFrame member variable, sets up the properties of the frame such as how big it should be and what kinds of components should be in it, and then shows itself.

The invokeLater method causes the Runnable to have its run method called in the dispatch thread of the AWT Event Queue. It won't happen until after all pending events are processed . That thread, the AWT Event Queue, stores events in a queue, and executes them asynchronously and sequentially (in the order stored).

Creating the Frame

Now let's look at how to make a main application window for storing buttons and text fields and other user controls. You do this using a JFrame, which you can give a title, as in the following example:

 

 JFrame myFrame = new JFrame("My Application"); //now call methods on myFrame to add controls, set //the layout, make it visible, and so forth 

Note that if you call the static method JFrame.setDefaultLookAnd FeelDecorated(true), this must be invoked before you call the JFrame's constructor, or it will have no effect. After you have a frame, there are a few maintenance things you need to set up. First, call frame.setDefaultClose Operation(JFrame.EXIT_ON_CLOSE) to ensure that when you click the X to close the window it will actually close.

Next, you need to call the pack() method, which fits all of the components into the available space according to their preferred size .

Finally, you call setVisible(true) in order to display the frame and its components.

Frame.runExample():

This code demonstrates creating, sizing, and showing an empty frame, similar to the Windows Form you get when you start Visual Studio.

FrameDemo.java
 

 package net.javagarage.demo.ui; /**<p>  * Very simple frame that does nothing.  * The purpose is to give you something to  * start with each time you make a new Swing app.  * </p>  * @author Eben Hewitt  **/ import java.awt.*; import javax.swing.*; public class FrameDemo {       JFrame frame;     //simply creates the frame, adds an empty     //label to it so that it has some size,     //and shows the frame     private static void launch() {     /* Change the way the frame looks and feels.      * decorate it with this command, instead of      * having the system do it. you must call this      * method BEFORE creating the JFrame.      */     JFrame.setDefaultLookAndFeelDecorated(true);    //create window, and put a title    //in top-left corner    frame = new JFrame("My App");    //make the app stop when window is closed frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    JLabel emptyLabel = new JLabel("");    emptyLabel.setPreferredSize(new Dimension(200, 200));    frame.getContentPane().add(emptyLabel,                               BorderLayout.CENTER);    //cause the window to be sized in such a way that    //it snugly fits the preferred size and layouts of    //the components in it. this is a method of    //the java.awt.Window class    frame.pack();    //show it    frame.setVisible(true);   } public static void main(String...args) { SwingUtilities.invokeLater(new Runnable() {    public void run() {        launch();    }    }); } } //end FrameDemo.java 

Figure 25-1. The basic frame will shut down the JVM when closed, and can be resized.

graphics/25fig01.gif


ContentPanel.getDefinition() The frame contains a content panel. The main content pane, which you get a reference to via frame.getRootContent Pane() is the workspace onto which you place buttons, text fields, and other elements with which the user directly interacts .

ContentPanel.getNote() The content panel is also called a pane, like a window pane. In the main, a pane can be plain. It is simply a plane.

There are different kinds of panes, however, including the JScrollPane, which automatically adds scrollbars, and the JTabbedPane. Both of these we make good use of in examples here.



Java Garage
Java Garage
ISBN: 0321246233
EAN: 2147483647
Year: 2006
Pages: 228
Authors: Eben Hewitt

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