Tuesday, May 24, 2011

Creating a Frame

Now that we know the history of Java Swings, lets get down to business. The first thing we are going to learn is “How to create a Frame”

So, lets get started!!

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.

Trivia:
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:


Don't lose heart. 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?


The code is pretty straight forward and is available below:
import java.awt.*;
import javax.swing.*;

/**
* @version 1.0 24-May-2011
* @author Anand Vijayakumar
*/
public class MyFirstFrame
{
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
FirstFrame frame = new FirstFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}
class FirstFrame extends JFrame
{
public FirstFrame()
{
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
}
public static final int DEFAULT_WIDTH = 300;
public static final int DEFAULT_HEIGHT = 200;
}


If you are new to swings, don't lose heart just yet. I am not going to leave you all by yourself to figure this out. Lets go through this code line by line.

The 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()
{
statements
}
});

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

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

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.

Trivia:
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.

Prev: Introduction to Java Swings

Next: Positioning a Frame

1 comment:

  1. Absolutely delighted to have found your site. Invaluable resource. Keep up the good work.

    JZR (Josh)

    ReplyDelete

© 2013 by www.inheritingjava.blogspot.com. All rights reserved. No part of this blog or its contents may be reproduced or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, or otherwise, without prior written permission of the Author.

ShareThis

Google+ Followers

Followers