Java: Example - ToUppercase

Level Introductory
Structure Subclass JApplet and define main
ComponentsJButton, JTextField
ContainersJApplet, JFrame
Layouts FlowLayout
Listeners ActionListener as named inner class

Dual applet and application

Applet. By extending JApplet, the class files that are produced can be used as an applet - the browser will call the constructor to create the applet.

Application. Any class that defines main can be used as an application. The main program creates a JFrame and creates an instance of itself (the applet) to use as the content pane.

  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 
 28 
 29 
 30 
 31 
 32 
 33 
 34 
 35 
 36 
 37 
 38 
 39 
 40 
 41 
 42 
 43 
 44 
 45 
 46 
 47 
 48 
 49 
 50 
 51 
 52 
 53 
 54 
 55 
 56 
 57 
 58 
// File   : GUI/components/calculators/toppercase/ToUpperCase.java 
// Purpose: Convert text in one field to uppercase in another.
//          Example of simple GUI.
//          Example of both application (has main) and applet (subclasses JApplet).
// Tag    : <applet code="ToUpperCase.class" height="63" width="589"></applet>
// Author : Fred Swartz - 2006-12-16 - Placed in public domain.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

//////////////////////////////////////////////////////////////// ToUpperCase
public class ToUpperCase extends JApplet {

    //=================================================== instance variables
    private JTextField _inField  = new JTextField(20);
    private JTextField _outField = new JTextField(20);

    //================================================================= main
    public static void main(String[] args) {
        JFrame window = new JFrame();
        window.setTitle("ToUpperCase Example");
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        //... JApplet works fine as content pane in a window!
        window.setContentPane(new ToUpperCase());

        window.pack();                       // Layout components.
        window.setLocationRelativeTo(null);  // Center window.
        window.setVisible(true);
    }

    //================================================== applet constructor
    public ToUpperCase() {
        //... Create or set attributes of components.
        _outField.setEditable(false);    // Don't let user change output.
        JButton toUpperButton = new JButton("To Uppercase");

        //... Add listener to button.
        toUpperButton.addActionListener(new UpperCaseAction());

        //... Add components directly to applet.  Don't need content pane.
        setLayout(new FlowLayout());
        add(_inField);
        add(toUpperButton);
        add(_outField);
    }

    /////////////////////////////////// inner listener class UpperCaseAction
    class UpperCaseAction implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            //... Convert text in one textfield to uppercase in another.
            String data = _inField.getText();  // Get the text
            String out  = data.toUpperCase();  // Create uppercase version.
            _outField.setText(out);            // Set output field
        }
    }
}

Possible extensions