Java Programming/Tutorials/Notepad

Creating a Notepad application

edit

You wouldn't be able to learn Java wholly if you don't practice working on sample programs. Throughout these tutorials we will help you grasp the true workability with Java projects. The first tutorial in the series is that of creating a simple Windows based application that resembles a notepad.

Coding the basics

edit

Setting the class structure

edit

To start programming on our example, we need to write down a class that will encapsulate our Notepad application. We begin with a simple class structure that is empty in the beginning and will be filled appropriately afterward.

Defining an entry point

edit
For an in-depth study into the structure of entry points, read the section on The Entry Point.

Because we need our class to be executed, we need an entry point to help it execute while it is stand-alone. To do so, we define a main(...) method that would take care of the execution code.

public class Notepad
{
    public static void main(String[] args)
    {
    }
}

Laying the base for a main frame

edit

The next step is to lay the foundation for a frame on which the Notepad application needs to be executed. For this, we import three generic packages namely java.awt.*, java.awt.event.* and javax.swing.*. Now, because we have javax.swing.* package imported in our code, we can safely extend our Notepad class with the JFrame class.

import java.awt.*;                     // AWT Components
import java.awt.event.*;               // AWT Events
import javax.swing.*;                  // Swing Components
public class Notepad extends JFrame
{
    public Notepad()
    {
    }
    public static void main(String[] args)
    {
    }
}

Using the properties of the frame

edit

Using the super() method, we try calling the super-class' constructor to access other properties and methods declared in the super-class. The constructor call that we made is an explicit call, that is, we specifically called for it. But, even if we hadn't called the super() method, it would implicitly (or automatically) be called.

In the code below, we try to access the methods inherited into the Notepad class in two different ways. Notice the this variable. The this variable, as you might have had studied, helps to call the variables that are a part of the class the variable is called within. The this variable used in this example is used to access the setSize(...) and the setDefaultCloseOperation(...) methods whilst within the class.

These two methods help in setting the size of the form and then introducing a DISPOSE_ON_CLOSE event that would dispose or destroy the frame object once the particular form is closed either by clicking the Close button or by pressing Alt+F4.

Whilst in the main(...) method, we initialize and instantiate the Notepad object. Now because we have a Notepad object named notepad with us, we can access public methods from its reference. We use the same object to call the setVisible(...) method. This would be the second way that we can use to access an object's methods and properties.

import java.awt.*;                        // AWT Components
import java.awt.event.*;                  // AWT Events
import javax.swing.*;                     // Swing Components
public class Notepad extends JFrame
{
    public Notepad()
    {
        super();
        this.setSize(300, 300);           // Set the frame size
        this.setDefaultCloseOperation(    // Automatically disposes the
          JFrame.DISPOSE_ON_CLOSE);       // frame when it closes
    }
    public static void main(String[] args)
    {
        Notepad notepad = new Notepad();  // Initialize and instantiate
                                          // a Notepad object
        notepad.setVisible(true);         // Displaying the form
    }
}

The code above is enough to display a blank frame that is of 300 pixel width and 300 pixel height aligned at the top-left corner of the desktop screen. Try closing the frame to check if the DISPOSE_ON_CLOSE code works. It should work! Then for a test, try taking out the setDefaultCloseOperation(...) method or just comment it to see what happens otherwise.