Results 1 to 4 of 4

Thread: Query regarding use of ActionListener in java

  1. #1
    Join Date
    May 2008
    Posts
    69

    Query regarding use of ActionListener in java

    Hi.

    I am getting confused about the use of ActionListeners in the java program.
    If I take multiple listeners in one program, is it valid?
    The I using one listener for each button,only if each button is in it's own inner class.
    In another program I'm using around 15 or more listeners in it. Is it anything invalid with programmer OR it is ok to use only one ActionListener in the program?

    Please suggest me..

  2. #2
    Join Date
    Oct 2005
    Posts
    2,393

    Re: Query regarding use of ActionListener in java

    I'm not confirmed about behavior of java, if you add a single object but multiple times to the same object. But I know this is completely unnecessary. Suppose if you have two different buttons, and if you want your program to listen to both buttons. For this you have to add your main program to the ActionListeners list of each button you have taken.

    Please make sure that your main program is not duplicating the actionsPerformed by two buttons(which you have defined) listening to each other.I hope this will solve your query.

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

    Re: Query regarding use of ActionListener in java

    Your method look harder to adapt ,should you wish to listen to the same command coming from different places.
    According to my Java skill ,creating a general listener makes it easier to take advantages of inheritance in java programming.

    For reference see below example:

    //Base class
    public class AppFrm extends JFrame implements ActionListener{

    public void actionPerformed(ActionEvent e){
    if ( e.getActionCommand().equals("Something") ){
    doSomething();
    }else if ( e.getActionCommand().equals("Else") ){
    doElse();
    }
    }
    }
    //Inherited Class
    public class NextFrame extends AppFrm implements ActionListener{
    public void actionPerformed(ActionEvent e){
    if ( e.getActionCommand().equals("Something") ){
    doSomthingElse();
    super.actionPerformed(e);
    }else{
    super.actionPerformed(e);
    }
    }
    }

  4. #4
    Join Date
    Feb 2008
    Posts
    1,852

    Re: Query regarding use of ActionListener in java

    In java ActionListener interface is used to receive events.The ActionListener interface extends EventListerner

    The class which is interested in processing an action event implements this interface, and the object created with that class is registered with a component, using the component's addActionListener method.

    actionPerformed method is invoked only when the event occurs.

    Syntax for this is:

    1. actionPerformed(ActionEvent e)

    2.public void actionPerformed(ActionEvent e)

Similar Threads

  1. Looking for Excel Web Query for Java Applet working sample
    By Omswaroop in forum MS Office Support
    Replies: 3
    Last Post: 17-01-2014, 10:04 AM
  2. Query in java classes
    By Aaliya Seth in forum Software Development
    Replies: 5
    Last Post: 26-02-2010, 01:34 AM
  3. Query with Date in java
    By Gunner 1 in forum Software Development
    Replies: 5
    Last Post: 12-02-2010, 03:55 AM
  4. Access ActionListener in java
    By Windowed in forum Software Development
    Replies: 3
    Last Post: 18-11-2009, 02:57 PM
  5. [JAVA] query results in a combobox and jtextfield
    By Aamin in forum Software Development
    Replies: 5
    Last Post: 31-03-2009, 02:20 PM

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,711,641,673.29183 seconds with 17 queries