Session 8 Lecture Notes for First Course in Java  

course homepage      course calendar

Applets and Event-driven programming in a Graphical User Interface (GUI)

AWT Package

Java provides the Abstract Windowing Toolkit (AWT) to support Graphical User Interfaces (GUIs) on any platform.

AWT provides most of the standard UI widgets that a user would expect, such as drop-down lists, labels, buttons, scrollbars, and checkboxes.

All the AWT widgets adopt the native look and feel. This has advantages and disadvantages.

Advantages: 

Disadvantages:

Contents of the AWT package

Frame

The Frame class is analogous to a window.

Component

The Component class is the base class for all the drawable items in a GUI.

Event-Driven Programming

An example of a user-level event is the user clicking a mouse button, moving the mouse, or pressing a key on the keyboard.

How events work

A sender fires a message to one or more listeners.

To receive events, listeners have to register themselves with the sender.

For example, a button has an addActionListener()method with takes as its argument an ActionListener instance. 
ActionListener is an interface that defines the actionPerformed()method, which takes as its argument an ActionEvent instance. 

Two Step Sequence

Step 1. The listeners who want to receive the event register themselves with the button object.

Step 2.When the user presses a button, the button object calls the actionPerformed method of each of its listeners.

Demonstration

The Unit Conversion Applet

---------------

About threads [notes not yet fully prepared]

Take advantage of the anonymous class to create a new thread. Your class overrides the abstract class, but you don't have to create a name for your class.

public void start()  {
   new Thread()  {
      public void run()  {
          while(!done)  {
          // do something
         }
     }
} // end of start

------

abstract class Thread

abstract public void run();

------

class MyThread
}
    public void run();
}

------

MyFirstApplet class and $.class  (an inner class because it's inside another class)

------

A note about grid bag layout:
if you use North, South, and Center, then North and South are like header and footer.

________________________
course homepage      course calendar