Friday, June 24, 2011

Swing : Creating a Frame

What is Frame?

A top-level window (i.e., a window that is not contained inside another window) is called a frame in Java. The AWT library has a class, called Frame, for this top level. The Swing version of this class is called JFrame and extends the Frame class. The JFrame is one of the few Swing components that is not painted on a canvas. Thus, the decorations (buttons, title bar, icons, and so on) are drawn by the user’s windowing system, not by Swing.

Most Swing component classes start with a “J”: JButton, JFrame, and so on. There are classes such as Button and Frame, but they are AWT components. If you accidentally omit a “J”, your program may still compile and run, but the mixture of Swing and AWT components can lead to visual and behavioural inconsistencies.

How would a Frame Look?

An empty or a simple frame will look like below:

This frame practically has no components in it. It is just a simple window that we have created with no components or fields in it.

What is the code to generate this Frame?

import java.awt.*;
import javax.swing.*;

public class MyFirstFrame
  public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
    public void run() {
      FirstFrame frame = new FirstFrame();
class FirstFrame extends JFrame
  public FirstFrame() {
  public static final int DEFAULT_WIDTH = 300;
  public static final int DEFAULT_HEIGHT = 200;

he first lines are the import statements. The Swing classes are placed in the javax.swing package. The package name javax indicates a Java extension package, not a core package. For historical reasons, Swing is considered an extension. However, it is present in every Java SE implementation since version 1.2.

By default, a frame has a rather useless size of 0 × 0 pixels. We define a subclass FirstFrame whose constructor sets the size to 300 × 200 pixels. This is the only difference between a FirstFrame and a JFrame. (We have set a size instead of 0 X 0)

In the main method of the Test class, we construct a FirstFrame object and make it visible.
There are two technical issues that we need to address in every Swing program.

First of all Swing components must be configured from the event dispatch thread, the thread of control that passes events such as mouse clicks and keystrokes to the user interface components. The following code fragment is used to execute statements in the event dispatch thread:

EventQueue.invokeLater(new Runnable()
  public void run()

I know that, this is too much to swallow in one bite, but stay with me here. For now just think of this as a magic wand that is used to start your Swing program. We will get into the details in the subsequent chapters.

Next, we define what should happen when the user closes the application’s frame. Since this is our first simple example, we just want to exit when the user clicks the close icon ‘X’. To make this happen, we use the statement:

In other programs with multiple frames, you would not want the program to exit just because the user closes one of the frames. By default, a frame is hidden when the user closes it, but the program does not terminate.

I know that it is kind of surprising that the program doesn't terminate when the user clicks the close icon. Unfortunately that is how Swing works and we have to manually write the code to exit.

Simply constructing a frame does not automatically display it. Frames start their life invisible. That gives the programmer the chance to add components into the frame before showing it for the first time. To show the frame, the main method calls the setVisible method of the frame.

After scheduling the initialization statements, the main method exits.

Did you notice something Surprising?

If you havent yet run the code I pasted above, I suggest you do it in order to be surprised.
Did you see that, even though the code in the main method has completed execution, the frame is still visible? This is unlike regular java programs that exit once the main method is done executing.

The reason is that, the event dispatch thread keeps the program alive until it is terminated, either by closing the frame or by calling the System.exit method.

No comments:

Post a Comment