GUI 3 - Tiny Window with subclass

Purpose of this lesson: New Java classes, constructors, and methods

OOP. This is a reimplementation of the TinyWindow program using the common style of building windows by defining a subclass of JFrame. This is the Object-Oriented Programming way -- when you want to take an existing class and "specialize" it or "extend" it by adding your own content and behavior, create a subclass of it, adding what you want. Your new subclass still has all the capabilities of its superclass, plus what you add.

This is an image of the window, dragged larger to show the entire title.

build the GUI in the constructor. The JFrame is built entirely in the constructor. You should use this style.

Main in JFrame subclass. It's common to put the main method in the JFrame subclass, but that can seem confusing at first because it builds an object of the class it's in. Because it's static, main can be in any class, but the most common location is in the JFrame subclass.

Source code

// File   : gui-tutorial/tw2/
// Purpose: Illustrate common JFrame subclass style for GUI
// Author : Fred Swartz, 2006-11-09, Placed in public domain.

import javax.swing.*;

////////////////////////////////////////////////////// class TinyWindow2
class TinyWindow2 extends JFrame {

    //====================================================== method main
    public static void main(String[] args) {
        TinyWindow2 window = new TinyWindow2();             //Note 1
        window.setVisible(true);                            //Note 2
    //====================================================== constructor
    public TinyWindow2() {                                  //Note 3
        //... Set window characteristics
        setTitle("Tiny Window using JFrame Subclass");      //Note 4
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);     //Note 5


  1. Create a new object (instance) of our JFrame subclass.
  2. Make window visible. This could also be the last line of the constructor.
  3. A "constructor" is like a method, and is used to initialize a class. You can tell the difference between a constructor and a normal method by two characteristics: (1) It has the same name as the class, and (2) It has no return type.
  4. Set the text on the window titlebar.
  5. Make sure the program stops when the window closes.

Calling methods in super / ancestor classes

When you extend an existing class, you may call the methods in the parent class, its parent etc. When calling from within this child / sub- class, you don't have to specify an object in front of calls as you normally would because the current object is assumed. If it makes the code clearer, use the keyword this to indicate that you are using the current object. It's never required, and therefore not usually written, but it sometimes it makes the code a little clearer. The following are identical.

setTitle("Tiny Window using JFrame Subclass"); 
this.setTitle("Tiny Window using JFrame Subclass");  // Same as above.

Next - Putting something in the window

Generally the window has a number of components in it. The next lesson shows how to add a component.