Java
  Home arrow Java arrow Page 6 - 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  
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 
Actuate Whitepapers 
VeriSign Whitepapers 
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 Part II
By: Gayathri Gokul
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 17
    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:
      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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Event Handling In Java Part II - Preparing to Launch Our Java Applet And Handle Events


    (Page 6 of 6 )

    Example 6: Save As RadioTest.java, Compile And View Using Appletviewer

    In this Applet example we examine MouseAdapters, and its methods like mouseClicked(). Plus ItemListener interface implementation and itemStateChanged() method and use getItem() method to display the item the user as selected in the Applet’s status bar using the showStatus()method. We will use interface components like checkbox, which are of two types-exclusive checkboxes (which means only one among the group can be selected) also called Radio Buttons. We also use non-inclusive checkboxes, which can be selected independently. The Choice class implements the pop-up menu that allows users to select items from a menu. This UI component dispalys the currently selected item with a arrow to its right.
    /*
    <applet code = "RadioTest.class" height = 300 width = 300 >
    </applet>
    */
    import java.awt.*;
    import java.awt.event.*;
    import java.applet.*;
    public class RadioTest extends Applet
    {
    public void init()
    {
    CheckboxGroup cbg = new CheckboxGroup();
    // Checkbox(label, specific checkgroup, checked:boolean)
    Checkbox c1 = new Checkbox("Black and White",cbg,false);
    Checkbox c2 = new Checkbox("Color",cbg,false);
    //adding mouselistener to the corresponding 
    // component to trap the event
    c1.addMouseListener(new check1());
    c2.addMouseListener(new check2());
    //adding components to the container
    add(c1);
    add(c2);
    //To create a Choice Menu(say to list the various choices)
    // a Choice Object is instantiated.
    // In short-Choice() constructor creates a new choice menu 
    //& you add items using addITem()
    Choice c = new Choice();
    c.add("LG");
    c.add("Onida");
    c.add("BPL");
    c.add("Samsung");
    c.add("Philips");
    c.add("Sony");
    // adding ItemListener to choice then adding it to the container
    c.addItemListener(new Ch());
    add(c);
    }
    Class check1 extends MouseAdapter
    {
    Public void mouseClicked(MouseEvent e)
    {
    showStatus("You have selected Black & White TV option");
    }
    }
    Class check2 extends MouseAdapter
    {
    Public void mouseClicked(MouseEvent e)
    {
    showStatus("You have selected Color TV option");
    }
    }
    Class Ch implements ItemListener
    {
    Public void itemStateChanged(ItemEvent e)
    {
    String s =(String)e.getItem();
    showStatus("You have selected" + s + " brand for your TV");
    }
    }
    }
    
    Hope you got a fair idea of what Event-Handling is all about. In addition, We have also dealt elaborately with Adapter classes and inner classes to help you write simplified codes on Java’s Event-Handling. If you still feel you haven’t got the groove of it, don’t worry read the article again and experiment with some codes on your own. For experience is the best teacher.
    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

     

       

    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

    BlackBerry VTS




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