Results 1 to 6 of 6

Thread: How to write an Action Listener in Java?

  1. #1
    Join Date
    Aug 2006
    Posts
    221

    How to write an Action Listener in Java?

    Hi friends,
    I have started doing Java before a month ago. I have done some basic coding into that. Now I want to implement an event handlers. I think to start with the action listener because it is very easy (read in my java book). So though that posting here would help me. Please explain me how to write an Action Listener in Java.?? It would be better for me to understand, if you provide some coding along with that.!!
    AMD Sempron 2800+ @ 2Ghz
    Asus A7V8X-LA
    120Gb Seagate barracuda 7200Rpm Ultra-ATA 100
    Elixir 512mb DDR Pc3200 (Soon 1Gb)
    Club 3D Radeon 9600 256Mb
    Lite-On Cd & Dvd writer combo
    IDE-Dvd drive
    400w Psu
    Windows Xp Pro Sp2
    Advent Wireless Mouse & Keyboard.

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    Re: How to write an Action Listener in Java?

    Even I think that the action listeners are the easiest event handlers to implement. Also you have to use this handler for many cases, so it is also used a lot. I think that before starting with the code you should know more about the action listeners. When you want to define what should be done when an user performs certain operation, then you will have to implement an action listener. Like when the user clicks a button, the result is that an actionPerformed message is sent to all action listeners. But before that you would have register on the relevant component.

  3. #3
    Join Date
    May 2008
    Posts
    2,389

    Re: How to write an Action Listener in Java?

    You can follow the steps that I have mentioned below for writing an action listener :
    1. The important and initial step is to declare an event handler class and along with that you must specify that whether the class either implements an ActionListener interface or extends a class that implements an ActionListener interface.
    2. Then once declare, you must register an instance of the event handler class as a listener on one or more components.
    3. The last step is to include the code that you have written which must implements the methods in listener interface.

  4. #4
    Join Date
    Jul 2006
    Posts
    289

    Re: How to write an Action Listener in Java?

    I think that you will find bit difficult to understand the steps mentioned by the 'opaper' because you might not have an idea of all that things mentioned over there. So I thought to help you by providing some useful samples of examples for that steps :
    You can declare an event handler class like in the following way :
    Code:
     public class MyClass implements ActionListener {
    Then you can register an instance of the event handler class in this way :
    Code:
    someComponent.addActionListener(instanceOfMyClass);
    Lastly include the code in the following method :
    Code:
    public void actionPerformed(ActionEvent e) { 
                  ...//code that reacts to the action... 
              }
    Signatures reduce available bandwidth

  5. #5
    Join Date
    Mar 2008
    Posts
    349

    Re: How to write an Action Listener in Java?

    I am providing you with an example of the writing an Action Listener in Java Here is the coding for the same :
    Code:
    import java.awt.*;
    import java.awt.event.*;
    
    public class actlis extends Frame implements WindowListener,ActionListener {
    	TextField text = new TextField(25);
    	Button b;
    	private int numClicks = 0;
    
    	public static void main(String[] args) {
    		actlis myWindow = new actlis("This is demo");
    		myWindow.setSize(410,110);
    		myWindow.setVisible(true);
    	}
    
    	public actlis(String title) {
    
    		super(title);
    		setLayout(new FlowLayout());
    		addWindowListener(this);
    		b = new Button("Click on Me");
    		add(b);
    		add(text);
    		b.addActionListener(this);
    	}
    
    	public void actionPerformed(ActionEvent e) {
    		numClicks++;
    		text.setText("Button Clicked " + numClicks + " times");
    	}
    
    	public void windowClosing(WindowEvent e) {
    		dispose();
    		System.exit(0);
    	}
    
    	public void windowOpened(WindowEvent e) {}
    	public void windowActivated(WindowEvent e) {}
    	public void windowIconified(WindowEvent e) {}
    	public void windowDeiconified(WindowEvent e) {}
    	public void windowDeactivated(WindowEvent e) {}
    	public void windowClosed(WindowEvent e) {}
    
    }

  6. #6
    Join Date
    Jul 2006
    Posts
    442

    Re: How to write an Action Listener in Java?

    The following is the action event handling code from an applet named Handler :
    Code:
    public class Handler ...  implements ActionListener {
    
        ...
    
        //where initialization occurs:
    
            button.addActionListener(this);
    
        ...
    
        public void actionPerformed(ActionEvent e) {
    
            Toolkit.getDefaultToolkit().beep();
    
        }
    
    }
    "When they give you ruled paper, write the other way..." J.R.J.

Similar Threads

  1. How to write a Document Listener in Java?
    By pushpendra in forum Software Development
    Replies: 8
    Last Post: 18-03-2010, 01:22 PM
  2. How to write a Key Listener in Java?
    By N I C K in forum Software Development
    Replies: 5
    Last Post: 13-02-2010, 01:52 AM
  3. How to write a Focus Listener in Java?
    By The Recruiter in forum Software Development
    Replies: 4
    Last Post: 12-02-2010, 07:05 AM
  4. How to write a Component Listener in Java?
    By hariharan_00 in forum Software Development
    Replies: 4
    Last Post: 12-02-2010, 06:28 AM
  5. How to write a Caret Listener in Java?
    By Dilbert in forum Software Development
    Replies: 5
    Last Post: 12-02-2010, 05:33 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,037,817.91403 seconds with 17 queries