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!

Action Listener Review

  • Many components call an action listener in response to a user action (JButton, JMenuItem, JTextField, etc). An action listener is any object that implements the ActionListener interface.
  • The ActionListener interface requires that the class define an actionPerformed(ActionEvent e) method.
  • Call the component's addActionListener(...) method, passing a parameter which is an object implementing ActionListener.
  • Common ways to implement action listeners follow.

1. One shared action listener (this) - Not great.

The class which defines the graphical user interface implements ActionListener. The component uses this for the action listener.

myButton.addActionListener(this);

Example p 234.

This doesn't scale -- what if there is more than one component that needs an action listener?

2. Anonymous inner class listener (so-so)

JButton b1 = new JButton("Hello");
b1.addActionListener(
    new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            // do something for button b1
        }
    }
);
  • Convenient.
  • Often mixes listeners into other code. (bad)
  • Can't share listeners.

3. Named inner class listener (better)

ActionListener myHelloListener = new HelloListener():

JButton b1 = new JButton("Hello");
b1.addActionListener(myHelloListener);
. . .
class HelloListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        // do something for button b1
    }
}
  • Relatively convenient.
  • Separates listeners from other code. (good)
  • Can easily share listeners.

4. Subclassing AbstractActionListener (very good)

AbstractAction exitAction = new AbstractAction() {
    public void actionPerformed( ActionEvent e ) {
        if (gCurrentSkeema.isDirty()) {
            int response = JOptionPane.showConfirmDialog(xc_window, 
                                       "Exit without saving changes?");
            if (response == JOptionPane.YES_OPTION) {
                System.exit(0);
            }
        } else {
            System.exit(0);  // Not dirty, can quit.
        }
    }
};

More on this later.

End

Ask programming questions?

 

 

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

Current Comments

1 comments so far (post your own) View All Comments Latest 10 Comments:

thanks for the help. I am a new java student and have found the example helpful.

how do I make a text area respond to the button's event this is what i have



import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;

public class appTrial extends Applet{
public void init(){
resize(400,200);
TextField text1=new TextField(30);
Label l1=new Label("Enter Age >");
Panel p1=new Panel();p1.setLayout(new FlowLayout(FlowLayout.LEFT));
p1.add(l1);
p1.add(text1);

TextField text2=new TextField(30);
Label l2=new Label("Total Price >");
Panel p2=new Panel();
p1.setLayout(new FlowLayout(FlowLayout.LEFT));
p2.add(l2);
p2.add(text2);

Button b1=new Button("Done");
b1.addActionListener(new ButtonHanler);
Button b2=new Button("Clear");
b2.addActionListener(new ButtonHndler);
Panel p3=new Panel();
p3.add(b1);
p3.add(b2);

Panel p4=new Panel();
p4.setLayout(new FlowLayout(FlowLayout.LEFT));

Panel main=new Panel();
main.setLayout(new GridLayout(4,2));
main.add(p4);
main.add(p1);
main.add(p2);
main.add(p3);
add(main);
}
}
public class MyActionListener implements ActionListener {
public String b1= "OK";
public String b2= "CANCEL";

public void actionPerformed (ActionEvent ae) {
Object source = ae.getSource();

/* Is it a JButton??? */
if (source instanceof JButton) {
JButton bu = (JButton)source;

/* Which JButton is it? */
String cmd = bu.getActionCommand();
if (cmd==b1){

} else
if (cmd==b2) {

}
}
}

}

Posted by ligami on Thursday, 09.13.07 @ 13:14pm | #26898

  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]