© 2001, 2002, 2003, 2004  by Daniel Berleant

 

 

Today’s topic: GUIs and events

 

Java uses an event driven approach to graphical user interfaces (GUIs)

It waits until an event occurs

The event causes an event handler method to run

The event handler does something

Your program does not call the event handler!

      So how does the event handler get called?

      The Java Runtime Environment (JRE) calls it

      (It is called by Java in response to a user action)

 

Let’s see how events work in an example:

First the basics

(then if time the swing modifications)

 

 

//(D. Berleant)

// Compiles and runs under 1.4

import java.lang.*;

import java.io.*;

import java.awt.*;

     //swing only

     //obsoletes some of this

import java.awt.event.*;

   //swing only obsoletes

   //some of above line

import javax.swing.*;

   

public class CharCheck{

   static Frame f;

   //swing version: JFrame f;

   public static void main(String args[ ]){

      DoSomething d=new DoSomething( );

      TextField t;

      /*swing version:

        JTextField t;

        JtextField is defined

        in javax.swing

      */

      t=new

        TextField

           ("Initial text");

      /*swing version:

        t=new  

          JTextField

            ("Initial text");

      */

      //Event handlers are in

      //java.awt.event...

      t.addActionListener(d);

      t.addKeyListener(d);

      f=new Frame

        ("Hello, I am" +

         "a Frame");

      /*swing version:

        f=new JFrame

          ("Hello, I am" +

           "a Frame");

      */

      f.setSize(800,500);

      f.setVisible(true);

      //Color is in java.awt

      f.setBackground

          (Color.orange);

      /* swing version:

         f.getContentPane().

           setBackground

            (Color.orange);

      */

      f.add("North",t);

      /* swing version

         f.getContentPane().

            add("North",t);

      */

      f.show( );

   }

}

class DoSomething extends KeyAdapter

           implements ActionListener{

   public void actionPerformed

     (ActionEvent e){

       System.out.println

         (e.getActionCommand( ));

   } //runs after typing

     //the return key

   public void keyPressed(KeyEvent k){

     System.out.println

     ("next char is "

      +(char)(1+k.getKeyChar( )));

   } //runs when a key is

     //pressed

   public void keyReleased(KeyEvent k){

      System.out.println

      ("number of char is "

       +(int)k.getKeyChar( ));

   } //runs when a key is

     //released     

}

Output is to

(1)     a frame, and

(2)     the window from which the program was run

Initial display:

 

After typing in some characters

(what characters? It’s fun but confusing to figure out)

System.out.println( ) output is:

 

 

More on Event Handlers

 

We needed two:

 

    Can't inherit more than one, so (at least) one must be an interface

 

    Java (JRE) knows what method to call when an event occurs

 

    That is why all interface method prototypes must be implemented

 

AWT vs. Swing

 

    AWT= "Abstract Windowing Toolkit"

    Contains many "heavyweight" components

        Component - a GUI item (buttons, text fields...)

        Heavyweight - refers to a GUI item that relies on the

                platform-provided windowing capabilities

        Problem: a Button looks different on Macs and MS systems

            - Java tries to be platform-independent!

        Lightweight - refers to a platform-independent

            GUI component

        Example: a JButton is lightweight

 

    Swing

        Has (mostly) lightweight components

        Used to be "experimental"

                   Hence,  import javax.swing.*;

    The "x" indicated it was experimental

          ...So, why is the "x" still there??

        If you use swing, must also import awt

        There are more swing components than awt components

            Example:

                TextField

       JTextField

       JPasswordField

                (no "PasswordField" class exists)

                - a JPasswordField is like

                  a JTextField but doesn't show

                  what characters are typed