Lecture 5A
  AWT Overview
  Components
  Containers
  Events
  Event Overview
  Event Example
  Event Processing
  Button Example
  Frames & Menus
  Other Components

  LayoutManagers
Lectures

1A: Introduction
1B: Java Intro
2A: BuildingBlocks
2B: Objects
3A: More Objects
3B: Exceptions
       & Threads
4A: Waves
4B: Nuclear &
       Particle
5A: AWT
5B: More AWT
      & Graphics
6A:Detectors &
      Simulation
6B: LHC/Atlas &
     RandomSims    
7A: Swing
7B: Java2D
8A: Java Apps
8B: Dialogs &
    MoreClasses
9A: Java I/O
9B: Utilities,
        Unicode
10A: More
    Threading
10B: File
    Handling
11A: Array,Print,
    Images
11B: SimplePhysics
    Simulation
12A: Tips &
    Techniques
12B: More Tips &
    Techniques
13A: Satellite
    
Simulations
13B: Intro to Java
    Networking
14A: Java Servers
14B: HTTP Server
15A: ServerClient
15B: ServerClient
   Expt.Simulation
16A: Course
          Review
16B: ExerciseTest
        Discussion

    Contacts
    Description
    Exercises
    Index
    Outline
    Q&A
    Resources
    StudentInfoForm
    Student Pages
    What's New

 

Home : Lectures : Lecture 5A : Event Example
Event Example

Here is an example of capturing mouse events!

It uses a Canvas and a TextArea. The Canvas provides a general drawing area. The TextArea provides for multi-line text editing and uses scrollbars if the text exceeds the visible area.

Example3.java

import java.awt.*;

import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;

public class Example3 extends java.applet.Applet
                        implements MouseListener {
  TextArea textArea;
  String newline;

  public void init() {

    setLayout(new BorderLayout() );

    Canvas c = new Canvas();
    add(c, "North");
    c.setSize(300,150);
    c.setBackground(Color.red);

    textArea = new TextArea();
    textArea.setEditable(false);
    add(textArea, "South");

    //Register for mouse events on the canvas.
    c.addMouseListener(this);

  }

  public void mousePressed(MouseEvent e) {
    saySomething("Mouse pressed; # of clicks: "
      + e.getClickCount(), e);
  }

  public void mouseReleased(MouseEvent e) {
    saySomething("Mouse released; # of clicks: "
      + e.getClickCount(), e);
  }

  public void mouseEntered(MouseEvent e) {
    saySomething("Mouse entered", e);
  }

  public void mouseExited(MouseEvent e) {
    saySomething("Mouse exited", e);
  }

  public void mouseClicked(MouseEvent e) {
    saySomething("Mouse clicked (# of clicks: "
      + e.getClickCount() + ")", e);
  }

  void saySomething(String eventDescription,
                     MouseEvent e) {
    textArea.append(eventDescription
                    + " detected on "            
            + e.getComponent().getClass().getName()
                        + "." + "\n");
  }
}

Note: the MouseListener interface has 5 methods that all must be overriden in the implementing class.

When one needs only one of the methods, the others must still must be encoded, albeit with empty code bodies.

This is rather inelegant. So the Java folk created for each of those listener interfaces with more than one method a corresponding adapter class that implements empty methods for each of the interface methods. A subclass of an adapter class then only needs to override the required method.

We will discuss adapter classes later in the context of inner classes.

 

Home Lectures Resources Index Contacts Students


Physics Simulations with JavaTM
KTH, Kurskod: 5A1418
Curator: Clark S. Lindsey