In this tutorial

You will learn how to...
  • How to create a JFrame.
  • To understand what a Look and Feel is.
  • To understand 'Thread-safe' GUIs.
  • To understand .setSize() and .pack();

JFrame - Part 1

In this first lesson we will start slowly and just introduce the concepts and code which you will use to create a JFrame.

A JFrame is the main window that you use to display the components you want to show on the screen.

Easy huh?

With that complicated technical description behind us, let's have a look at some code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import javax.swing.*;
// Importing the package that contains all the Swing Components and Classes.
    
public class FrameExample {
    
    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
    
    private static void createAndShowGUI() {
        //Create and set up the frame. 
        //The string passed as an argument will be displayed 
        //as the title.
    
        JFrame frame = new JFrame("[=] Hello World [=]");
    
        //Display the window.
        frame.setSize(400, 100);
        frame.setVisible(true);
    }  
}
Although the words look new, the concepts and shape of the code should not be a surprise to you. A great deal of the code is very intuitive when you get the basic concepts. Let's split the code down into bite-size chunks and have a go at understanding them.
1
import javax.swing.*;
This is the package that contains all the Swing Components and Classes, so we need to import it into our program.
4
public class FrameExample {
The class we are creating is called FrameExample. This shouldn't be very different to any of the Java coding you've done before. Within this class, there are two methods; createAndShowGUI() and main().
6
7
8
9
10
11
12
13
14
    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
main() should be familiar to you, it is the primary method and where the execution of the program begins.
The code within it is a little tricky, but important. It uses a class within the Swing package to schedule a job within the event-dispatching thread. This job makes sure your GUI is created and showed on the screen.

It is important the GUI is executed using this method so that the GUI is 'thread-safe'.

In simple terms, a thread is a thread of execution i.e. a task that is happening. Modern processors can handle multiple threads (lots and lots of them). Each thread may or may not use the same address space and processor. When a GUI is thread-safe, it will not slow or disappear when simultaneous executions of multiple threads are happening, and it will not hog or control resources that may slow down other threads.

If that makes sense, good. If it doesn't, have a look at Wikipedia for a bit more information if you particularly want, but don't worry about it too much. Just understand that it is important that the GUI is displayed in this way.

So, main calls the function createAndShowGUI() in a way that is thread-safe. Woo. We've still not got to JFrames yet, so...
16
17
18
19
20
21
22
23
24
25
26
    private static void createAndShowGUI() {
        //Create and set up the frame. 
        //The string passed as an argument will be displayed 
        //as the title.
    
        JFrame frame = new JFrame("[=] Hello World [=]");
    
        //Display the window.
        frame.setSize(400, 100);
        frame.setVisible(true);
    }  
createAndShowGUI() is where we actually create the JFrame. We create a JFrame just as we would almost any other Object. The String that we pass as an argument when we create the JFrame object is the title of the JFrame. You'll see in a moment.
21
        JFrame frame = new JFrame("[=] Hello World [=]");
That's the JFrame created, but now we have to display it. By default the JFrame is 0 pixels wide by 0 pixels long, and you can't see it. So, we set the size to the size we want. Here the JFrame is now 400px wide (x-axis) and 100px high (y-axis).
24
        frame.setSize(400, 100);
We will go into visibility at another time, but setVisible() is used to quickly hide and show components. By default, the JFrame is not shown and its visibility is false. We want to show the JFrame so we set the visibility to true.
25
        frame.setVisible(true);
When you compile the code given at the start, you should get something that looks a little like this. (might be a bit different if you are on Linux, this picture was taken on Windows)

Picture of JFrame Example in Windows OS

Congratulations, you have successfully displayed your first JFrame! You are well on your way to becoming a competent GUI developer. However, you are not there yet, there are still some little things you should do to your JFrame for it to begin to look professional.

Back Top Next
Email Me
Code Style


Required Lessons

External Links

Created and Edited by Stuart Davidson
All Rights Reserved ©

Valid XHTML 1.0 Strict