Home arrow Java & J2EE arrow Page 4 - Event Handling In Java Part II

Easy Steps For Coding - Java

With the skills that you have developed so far from Part I of the tutorial, you can design a graphical user interface with beauty and easy. Let us refresh ourselves before we proceed, Events are method calls that Javas windowing system performs whenever any element of a user interface is manipulated.

  1. Event Handling In Java Part II
  2. Explicit-Event Handling
  3. Open The Window And Inhale Java In The Air
  4. Easy Steps For Coding
  5. A Glimpse Of Adapters In Java
  6. Preparing to Launch Our Java Applet And Handle Events
By: Gayathri Gokul
Rating: starstarstarstarstar / 23
September 15, 2003

print this article


Example 2: Save as MyFrame.java.

• Create a separate listener class that implements the windowListener interface.

• Since WindowListener is an interface, you will have to define all the methods that are declared in it.

• Add the code for the specific event that you want your program to handle.

• Register the listener object for the window using the addWindowListener() method.

Import java.awt.*;
Import java.awt.event.*;
Class OurWindowListener implements windowListener
//Event handler for the window closing event
Public void windowClosing (windowEvent we)
Public void windowClosed (windowEvent we)
Public void windowOpened (windowEvent we)
Public void windowActivated (windowEvent we)
} Public void windowDeactivated (windowEvent we)
Public void windowIconified (windowEvent we)
Public void windowDeiconified (windowEvent we)
Public class MyFrame extends Frame
Button b1;
// Main Method
Public static void main (String arg[])
MyFrame f = new MyFrame();
//Constructor for the event derived class
Public MyFrame()
Super (“Windows Events-Title”);
b1 = new button(“Click Me”);
//place the button object on the window
//Register the listener for the button
ButtonListener listen = new ButtonListener();
//Register a listener for the window.
OurWindowListener wlisten = new OurWindowListener();
//display the window in a specific size
}//end of frame class
//The Listener Class
Class ButtonListener implements ActionListener
//Definition for ActionPerformed() method
Public void ActionPerformed(ActionEvent evt)
Button source = (Button)evt.getSource();
Source.setLabel(“Button Clicked, Buddy!”);
In the above example MyFrame class makes a call to the addWindowListener() method, which registers object for the window. This enables the application to handle all the window-related events. When the user interacts with the application by clicking close button, maximizing or minimizing a WindowEvent object is created and delegated to the pre-registered listener of the window. Subsequently the designated event-handler is called.

In the above example 2, the class OurWindowListener has methods that do not contain any code. This is because the windowListener interface contains declarations for all these methods forcing you to override them. Java’s Adapter classes provide a handy solution to this problem.

>>> More Java & J2EE Articles          >>> More By Gayathri Gokul

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- More Java Bugs Lead to More Attacks
- Oracle's Java One Brings News, Surprises
- Oracle Patches Java Runtime Environment
- Apple Syncs Java Update with Oracle
- Spring 3.1 Java Development Framework Compat...
- Jelastic Java PaaS Availability and Pricing ...
- NetBeans 7.1 Released, Supports JavaFX 2
- SolarWinds Releases Newest Version of Java M...
- Free Monitoring Tool for Java Apps on Heroku
- Heroku Adds JCloud Platform Support, Java 7 ...
- Java SE 8 Speculation in Full Swing
- Java SE 7 Now Available
- New JVM Language and Java Reporting Tool
- Java 7 Release Update and New Eclipse Toolkit
- The Best Java Netbeans IDE Plugins

Developer Shed Affiliates


Dev Shed Tutorial Topics: