Java
  Home arrow Java arrow Page 4 - Event Handling In Java Part II
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
JAVA

Event Handling In Java Part II
By: Gayathri Gokul
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 20
    2003-09-15


    Table of Contents:
  • Event Handling In Java Part II
  • Explicit-Event Handling
  • Open The Window And Inhale Java In The Air
  • Easy Steps For Coding
  • A Glimpse Of Adapters In Java
  • Preparing to Launch Our Java Applet And Handle Events

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Event Handling In Java Part II - Easy Steps For Coding
    ( Page 4 of 6 )

    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)
           {
    	             System.exit(0);
            }
     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
                  add(“center”,b1);
    
                  //Register the listener for the button
                    ButtonListener listen = new ButtonListener();
                     b1.addActionListener(listen);
    
                  //Register a listener for the window.
                   OurWindowListener wlisten = new OurWindowListener();
                   addWindowListener(wlisten);
    
                   //display the window in a specific size
                    setVisible(true);
                    setSize(200,200);
            }//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 Articles          >>> More By Gayathri Gokul
     

       

    JAVA ARTICLES

    - Exception Handling Techniques in Java
    - More About Multithreading in Java
    - The Basics of Multiple Threads in Java
    - Data Access Using Spring Framework JDBC
    - New Object Initialization in Java
    - Adding Images With iTextSharp
    - Adding Columns With iTextSharp
    - Creating Simple PDF Files With iTextSharp
    - The Spring Framework: Understanding IoC
    - Introducing the Spring Framework
    - Java Classes
    - Completing the Syntactic Comparison of Java ...
    - Syntactic Comparison of Java and C/C++
    - Java Statements
    - Conditionals, Expressions and Other Java Ope...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek