Java
  Home arrow Java arrow Page 4 - Event Handling In Java
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Moblin 
JMSL Numerical Library 
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? 
JAVA

Event Handling In Java
By: Gayathri Gokul
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 120
    2003-09-11

    Table of Contents:
  • Event Handling In Java
  • Going Into Over Drive
  • Relax Back Event Handling In A Nut Shell
  • Event-Handling In Java
  • Amazingly Simple Applets
  • Naughty Examples For Those Little Mice
  • A Quick Recap

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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 - Event-Handling In Java


    (Page 4 of 7 )

    ActionEvent using the ActionListener interface: The following illustrates the usage of ActionEvent and ActionListener interface in a Classic Java Application (Example I).

    //Save the file with MyEvent.java file and compile it using javac, 
    //once complied errors free execute it.
    Import javax.swings.*;
    Import java.awt.event.*;
    Public class MyEvent extends JFrame
    {
    JButton b1;
    // Main Method
    Public static void main (String arg[])
    {
    MyEvent event = new MyEvent();
    }
    //Constructor for the event derived class
    Public MyEvent()
    {
    Super(“Window Title: Event Handling”);
    b1 = new Jbutton(“Click Me”);
    //place the button object on the window
    getContentPane().add(“center”,b1);
    //Register the listener for the button
    ButtonListener listen = new ButtonListener();
    b1.addActionListener(listen);
    //display the window in a specific size
    setVisible(true);
    setSize(200,200);
    }
    //The Listener Class
    Class ButtonListener implements ActionListener
    {
    //Definition for ActionPerformed() method
    Public void ActionPerformed(ActionEvent evt)
    {
    JButton source = (JButton)evt.getSource();
    Source.setText(“Button Has Been Clicked, Guru!”);
    }
    }
    }
    
    How does the above Application work?

    • The execution begins with the main method.

    • An Object of the MyEvent class is created in the main method.

    • Constructor of the MyEvent class is invoked.

    • Super () method calls the constructor of the base class and sets the title of the window as given.

    • A button object is created and placed at the center of the window.

    • A Listener Object is created.

    • The addActionListener() method registers the listener object for the button.

    • SetVisible () method displays the window.

    • The Application waits for the user to interact with it.

    • When the user clicks on the button labeled “Click Me”: The “ActionEvent” event is generated. Then the ActionEvent object is created and delegated to the registered listener object for processing. The Listener object contains the actionPerformed() method which processes the ActionEvent In the actionPerformed() method, the reference to the event source is retrieved using getSource() method. The label of the button is changed to “Button has been clicked, Guru!” using setText() method.

    Tools of the Trade: Since the ButtonListener class has been declared under MyEvent class. Therefore ButtonListener class is an inner class.

    More Java Articles
    More By Gayathri Gokul


     

       

    JAVA ARTICLES

    - 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...
    - Java Operators
    - Primitive Data Types and Basic Language Rule...
    - Java and Object-Oriented Programming
    - Java Beginning Programming
    - Gaming Development Setup





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway