Main Page

Previous Next

Creating a Window

A basic window in Java is represented by an object of the class Window in the package java.awt. Objects of the class Window are hardly ever used directly since borders and a title bar are fairly basic prerequisites for a typical application window, and this class provides neither. The library class JFrame, defined in javax.swing, is a much more useful class for creating a window, since as well as a title bar and a border, it provides a wealth of other facilities. Its superclasses are shown below.

Click To expand

The Component class is the grandmother of all component classes – it defines the basic properties and methods shared by all components. We will see later that all the Swing components have the Component class as a base. The Container class adds the capability for a Component object to contain other components, which is a frequent requirement. Since JFrame has Container as a superclass, a JFrame object can contain other components. Beyond the obvious need for a window to be able to contain the components that represent the GUI, a menu bar should contain menus, for instance, which in turn will contain menu items: a toolbar will obviously contain toolbar buttons, and there are many other examples. For this reason the Container class is also a base for all the classes that define Swing components.

The Window class adds methods to the Container class that are specific to a window, such as the ability to handle events arising from user interaction with the window. The Frame class is the original class in java.awt that provided a proper window, with a title bar and a border, with which everyone is familiar. The JFrame class adds functionality to the Frame class to support much more sophisticated facilities for drawing and displaying other components. You can deduce from the hierarchy in the diagram how a JFrame object can easily end up with its 200+ methods as it has five superclasses from which it inherits members. We aren't going to trawl through all these classes and methods. We'll just look into the ones we need in context as we go along, and then see how they are applied for real. This will hopefully teach you the most important methods in this class.

You can display an application window simply by creating an object of type JFrame, calling a method for the object to set the size of the window, and then calling a method to display the window. Let's try that right away.

Try It Out – Framing a Window

Here's the code:

import javax.swing.JFrame;

public class TryWindow {
  // The window object
  static JFrame aWindow = new JFrame("This is the Window Title"); 

  public static void main(String[] args) {
    int windowWidth = 400;                              // Window width in pixels
    int windowHeight = 150;                             // Window height in pixels
    aWindow.setBounds(50, 100,                          // Set position
                      windowWidth, windowHeight);       // and size
    aWindow.setVisible(true);                           // Display the window

Under Microsoft Windows, the program will display the window shown:

Click To expand

Try resizing the window by dragging a border or a corner with the mouse. You can also try minimizing the window by clicking on the icons to the right of the title bar. Everything should work OK so we are getting quite a lot for so few lines of code. You can close the application by clicking on the close icon.


This example will terminate OK if you have entered the code correctly, however errors could prevent this. If an application doesn't terminate properly for any reason you will have to get the operating system to end the task. Under MS Windows, switching to the DOS window and pressing Ctrl+C will do it.

How It Works

The import statement adds JFrame in the package javax.swing to our program. From now on most of our programs will be using the components defined in this package. The object of type JFrame is created and stored as the initial value for the static data member of the class TryWindow, so it will be created automatically when the TryWindow class is loaded. The argument to the constructor defines the title to be displayed in the application window.

The main() method calls three methods for the aWindow object. The method setBounds()defines the size and position of the window; the first pair of arguments correspond to the x and y coordinates of the top-left corner of our application window relative to the top-left corner of the display screen, and the second pair of arguments specify the width and height of the window in pixels. The screen coordinate system has the origin point, (0, 0), at the top-left corner of the screen, with the positive x-axis running left to right and the positive y-axis from top to bottom. The positive y-axis in screen coordinates is therefore in the opposite direction to that of the usual Cartesian coordinate system.

Click To expand

We have specified the top-left corner of our application window at position (50, 100) on the screen, which will be 50 pixels to the right and 100 pixels down. Since the window will be 400 pixels wide and 150 pixels high, the bottom right corner will be at position (450, 250). The actual physical width and height of the window as well as its position relative to the edge of the screen will depend on the size of your screen and the display resolution. For a given screen size, the higher the display resolution, the smaller the window will be and the closer it will be to the top left-hand corner, simply because the pixels on the screen will be closer together. We will see how we can get around this potential problem later in this chapter.

The setDefaultCloseOperation() method determines what happens when you close the window by clicking on either the close icon or selecting Close from the menu that is displayed when you click on the in the top-left corner of the window. There are four possible argument values you can use here. The constant we have used at the argument to the method, EXIT_ON_CLOSE, is defined in the JFrame class. The effect of this is to close the window, dispose of the window resources and those of any components it contains, and finally to terminate the application. There are three other argument values you could use with the setDefaultCloseOperation() method that are defined in the WindowConstants interface. These values are:




This causes the frame and any components it contains to be destroyed but doesn't terminate the application.


This makes the close operation for the frame window ineffective.


This just hides the window by calling its setVisible() method with an argument of false. This is the default action if you don't call the setDefaultCloseOperation() method with a different argument value. When a window is hidden, you could always display the window again later by calling setVisible() with an argument of true.

Of course, you may want to take some action beyond the options we have discussed here when the user chooses to close the window. If the program involves entering a lot of data for instance, you may want to ensure that the user is prompted to save the data before the program ends. This involves handling an event associated with the close menu item or the close button, and we will be investigating this in the next chapter.

The method setVisible() with the argument set to true, displays our application window on top of any other windows currently visible on the screen. If you wanted to hide the window somewhere else in the program, you would call setVisible() with the argument set to false.

It's a very nice window, but not overly useful. All you can do with it is move, resize, and reshape it. You can drag the borders and maximize and minimize it. The close icon works because our program elected to dispose of the window and exit the program when the close operation is selected by setting the appropriate option through the setDefaultCloseOperation() method. If we omitted this method call, the window would close but the program would not terminate.

The setBounds() and setVisible() methods are members of the JFrame class inherited from the Component class, so these are available for any component. However, you don't normally set the size and position of other components, as we will see. The setDefaultCloseOperation() method is defined in the JFrame class so this method only applies to JFrame window objects.

Before we expand our JFrame example we need to look a little deeper into the make-up of the component classes.

Previous Next
JavaScript Editor Java Tutorials Free JavaScript Editor