Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Spring Framework | Web Services | BioInformatics | Java Server Faces | Jboss 3.0 tutorial | Hibernate 3.0 | XML

Tutorial Categories: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML


 

Java Tutorials


 

 

Struts Tutorials

Struts Resources

Visit Forum! Post Questions!
Jobs At RoseIndia.net!

Java Notes

Top-level Listeners

 

This is an example of top-level Listeners in Java.

 

In this example you will learn how to create top level-listeners in swing applications.

 

Using this as a listener

 

A common way to write simple applets is to use the use the applet itself as a listener (referred to as this).

 

For example

 

...
public class Hello extends JApplet implements ActionListener {
   JButton b;
   ...
   public void init() {
      JButton b = new JButton("Hello");
      b.addActionListener(this);
      ...
   }
   
   public void actionPerformed(ActionEvent e) {
      message = "Hi";
      repaint();
   }
   ...
}

This doesn't seem to be a problem for small programs, but what happens if there is more than one button? There can be only one actionPerformed() method in a class.

Problem: One listener for many components

Inside the listener method, it's possible to check the parameter to find out which component caused the event. For example,

JButton b1, b2;
. . .
public MyClass() {  // constructor
    ...
    b1.addActionListener(this);
    ...
    b2.addActionListener(this);  // SAME listener!
    ...
}//end constructor
 
public void actionPerformed(ActionEvent e) {
    Object obj = e.getSource();    // get the control that caused the event
    if (o instanceof JButton) {    // make sure it's a button.
        JButton b = (JButton)obj;  // downcast to a button
        if (b == b1) {   // UGLY, DON'T DO THIS
           // do something for button b1
        } else if (b == b2) {
           // do something for button b2
        }
    } else if (o instanceof JTextField) {
       ...
    ...

Using one listener makes the response slower, and forces all events to be handled in the same place. This uses the event model which was introduced in Java 1.1, but it has all the problems of the old Java 1.0 event model. Altho you will see this style used in some Java books, don't use it. It doesn't scale up from one control to many. Buttons, menu items, toolbar items, etc use action listeners; imagine what this method would look like for Microsoft Word!

Grouping all separate action listeners together in the source code is a good idea to make the code more comprehensible, but don't try to put all processing in one method!

Solution: Use inner class listeners

See Anonymous Listeners.

Ask programming questions?

 

 

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

0 comments so far (post your own) View All Comments Latest 10 Comments:
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification

Tell A Friend
Your Friend Name

 

 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright 2007. All rights reserved.

[an error occurred while processing this directive]