Java
  Home arrow Java arrow Page 5 - 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 - A Glimpse Of Adapters In Java
    ( Page 5 of 6 )

    Adapter Classes:

    Java provides us with adapter classes that implement the corresponding listener interfaces containing one or more methods. The methods in these classes are empty. The Listener class that you define can extend the Adapter class and override the methods that you need. The adapter class used for WindowListener interface is the WindowAdapter class.

    So you can simplify the above code (example 2) using Adapter class in the following manner:

    Example 3:Save as MyFrames.java and complie.
    Import java.awt.*;
    Import java.awt.event.*;
    Class MyFrames extends frame
    {
         Public static void main(String arg[])
         {
              MyFrames f = new MyFrames();
          }
           //constructor of the Frame derived class
          Public MyFrames
           {
                    //Register the Listener for the window
                      super(“The Window Adapter Sample”);
                      MyWindowListener mlisten = new MyWindowListener();
                      addWindowListener(mlisten);
                       setVisible(true);
             }
    }
    
    Class MyWindowListener extends WindowAdapter
    {
          //event handler for windows closing event
           Public void windowClosing(WindowEvent we)
            {
                  MyFrames f;
                   f = (MyFrames)we.getSource();
                   f.dispose();
                   System.exit(0);
           }
      }
    
    The Following is a list of Adapter classes and Listener Interfaces In Java:
    Event Category Interface Name Adapter Name Method
    Window             Window Listener       Window Adapter       Void windowClosing (WindowEvent e)
    Void windowOpened (WindowEvent e)
    Void windowActivated (WindowEvent e)
    Void windowDeactivated (WindowEvent e)
    Void windowClosed (WindowEvent e)
    Void windowIconified (WindowEvent e)
    Void windowDeiconified (WindowEvent e)
    Action ActionListener   Void actionPerformed(ActionEvent e)
    Item ItemListener   Void itemStateChanged(ItemEvent e)
    Mouse Motion  MouseMotionListener  MouseMotionAdapter  Void mouseDragged(MouseEvent e)
    Void mouseMoved(MouseEvent e)
    Mouse Button     MouseListener      MouseAdapter     Void mousePressed(MouseEvent e)
    Void mouseReleased(MouseEvent e)
    Void mouseEntered(MouseEvent e)
    Void mouseClicked(MouseEvent e)
    Void mouseExited(MouseEvent e)
    Key   KeyListener   KeyAdapter   Void keyPressed(KeyEvent e)
    Void keyReleased(KeyEvent e)
    Void keyTyped(KeyEvent e)
    Focus  FocusListener     Void focusGained(FocusEvent e)
    Void focusLost(FocusEvent e)
    Component    ComponentListener    ComponentAdapter    Void componentMoved(ComponentEvent e)
    Void componentResized(ComponentEvent e)
    Void componentHidden(ComponentEvent e)
    Void componentShown(ComponentEvent e)
    {mospagebreak title=Dissecting Java As Far As Inner Classes} Inner Classes:

    Inner classes are classes that are declared within other classes. They are also knows as nested classes and provide additional clarity to the program. The scope of the inner class is limited to the class that encloses it. The object of the inner class can access the members of the outer class. While the outer class can access the members of the inner class through an object of the inner class.

    Syntax:
     class 
    {
          class 
           {
           }
           //other attributes and methods
    }
    
    Example: 4 Save as MyFrame.java then compile and excute the program.
    Import java.awt.*;
    Import java.awt.event.*;
    Class MyFrame extends Frame
    {
          //inner class declaration
          class MyWindowListener extends MyAdapter
          {
              //event handler for windows closing event
           Public void windowClosing(WindowEvent w)
            {
                  MyFrame frm;
                   frm = (MyFrames)w.getSource();
                   frm.dispose();
                   System.exit(0);
           }
     
         Public static void main(String arg[])
         {
              MyFrame frm = new MyFrame();
          }
          
          //constructor of the Frame class
          Public MyFrames
           {
                    //Register the Listener for the window
                      super(“Illustration For Inner or Nested Classes”);
                     //creating an object of inner class
                      MyWindowListener wlisten = new MyWindowListener();
                      addWindowListener(wlisten);
                       setVisible(true);
                       setSize(100,100);
             }
    }
    
    
    The above example code declares an object of the inner class in the constructor of an outer class. To create an object of the inner class from an unrelated class, you can use the new operator as if it were a member of the outer class.

    Example:5
    MyFrame frame = new MyFrame(“Title”);
    Frame.MyWindowsListener listen = new MyFrame().MyWindowListener();
    
    You can create a class inside a method. The methods of the inner class can have access to the variables define in the method containing them. Inner class must be declared after the declaration of the variables of the method so those variables are accessible to the inner class.

    Now that we have seen elaborately on Java’s Event-Handling and Adapter Classes and Inner Classes let us play with a few examples to help you understand better and see if we got a feel of everything we learned today. Don’t panic we take it each step at a time.

     
     
    >>> 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 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek