Data Structures—Applet Example

Here’s a quick example of a simple web-deployed applet based on a Swing JApplet.

To test the applet, type a (relatively small, non-negative) number into the text field at the top, then click the button labelled Enter. You should see all the numbers from 0 up ton-1, where n is your number, listed in the multi-line text area below.

You can see the source code to the JApplet below; this JApplet was built “by hand”; we would normally build a GUI interface from now on using NetBean’s interface builder.


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

public class Form extends JApplet {

    public Form() {
    public final void initUI() {


        // ---------------------------------------
        final JTextField input = new JTextField();
        input.setBounds( 50, 25, 80, 25);

        // ---------------------------------------
        final JButton enter = new JButton("Enter");
        enter.setBounds(150, 25, 80, 25);

        // --------------------------------------
        final JTextArea output = new JTextArea();
        output.setBounds(50, 75, 300, 200);
        output.setBorder(new BevelBorder(BevelBorder.RAISED));
        enter.addActionListener( new ActionListener() {
            public void actionPerformed(ActionEvent e)
                try {
                  int n = Integer.parseInt(input.getText());
                  for(int i=0; i<n; i++)
                    output.append(i + "\n");
                catch (NumberFormatException nfe) {
                  output.setText("Dude, that's not a number!");
        ) ;