Results 1 to 4 of 4

Thread: Access ActionListener in java

  1. #1
    Join Date
    Nov 2009
    Posts
    343

    Access ActionListener in java

    Hi
    I have a problem in my code. I have posted it below. In the code I have two classes.
    Code:
    class 1:
    ..
    ..
    ..
    private class Listener implements ActionListener
        {   public void actionPerformed(ActionEvent e)
                {type = e.getActionCommand();}}
    ..
    ..
    class 2:
    ..
    ..
    ..
    private class AddListener implements ActionListener
    {   public void actionPerformed(ActionEvent e)
            {  Video video = new Video(?);    }}
    ..
    ..
    I don't know how to refer to the ActionListener class of class 1 from the ActionListener class of class 2. Please help me.

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

    Re: Access ActionListener in java

    Hi,
    I think you have declared the video inside the method, so it will be visible inside that method only. If it is not inside class2 method you can not access it. Please check your program again. Try it you self, if it is not possible we will help you. Next time post your updated code so that we can review it.

  3. #3
    Join Date
    Nov 2009
    Posts
    343

    Re: Access ActionListener in java

    Hi
    Thanks for the reply. The other way I was thinking to get rid of the Action Listener in class1. By make that change I have to store the button that was clicked in a method and access that method from Action Listener in the class2. This is just my thinking, I do not know if this will work. Any suggestions on this, please advice.

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

    Re: Access ActionListener in java

    Hi
    I think your code should be something like this, see the posted code below
    Code:
    class 1:
    private String type = "";
    ..
    ..
    ..
    private class Listener implements ActionListener
        {   public void actionPerformed(ActionEvent e)
                {type = e.getActionCommand();}}
    
    public String getType() {
      return type;
    }
    ..
    ..
    Code:
    class 2:
    private Class1 class1Reference;
    public void setClass1(Class1 class1Reference) {
      this.class1Reference = class1Reference;
    }
    ..
    ..
    ..
    private class AddListener implements ActionListener
    {   
      public void actionPerformed(ActionEvent e) {  
        String type = class1Reference.getType();
        Video video = new Video(type);    
      }
    }
    ..
    ..
    If any more queries do post back. And even if solved post, what did you do to solve it.

Similar Threads

  1. Pass JFrame to actionlistener
    By Freeza 2 in forum Software Development
    Replies: 5
    Last Post: 23-08-2010, 06:30 PM
  2. Where to use actionlistener ?
    By Anthony12 in forum Software Development
    Replies: 7
    Last Post: 09-08-2010, 10:20 AM
  3. Add actionlistener to JButton
    By KAMANA in forum Software Development
    Replies: 6
    Last Post: 03-08-2010, 10:54 AM
  4. File access with Java
    By Alejandro in forum Tips & Tweaks
    Replies: 2
    Last Post: 21-07-2010, 04:51 AM
  5. Query regarding use of ActionListener in java
    By beelow in forum Software Development
    Replies: 3
    Last Post: 16-11-2009, 10:56 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,711,756,654.50858 seconds with 16 queries