Results 1 to 4 of 4
  1. #1
    jboy is offline Member
    Join Date
    Aug 2009
    Posts
    56
    Rep Power
    0

    Default How to access the ActionListener

    Hi,

    I have two classes and each class has an ActionListener class within it.
    Java 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(?);    }}
    ..
    ..
    My problem is that I don't know how to refer to the ActionListener class of class 1 from the ActionListener class of class 2, so that I can put the "type"
    from the first ActionListener into the "Question mark" in the second ActionListener.

    Thanks in advance.

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    10

    Default

    You can't. The video is declared inside the method so it is only visible inside that method only. You can't even access it from class 2 itself when not inside that method. Relook your design.

  3. #3
    jboy is offline Member
    Join Date
    Aug 2009
    Posts
    56
    Rep Power
    0

    Default

    The only other way that I think of doing this is to get rid of the ActionListener in class 1, and I have to store the Button that was clicked in a method and access that method from the ActionListener in the class 2. But I don't know how to go about finding out which button is clicked without using any listener if we follow what I just described.
    Thanks.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    28

    Default

    Java Code:
    class 1:
    private String type = "";
    ..
    ..
    ..
    private class Listener implements ActionListener
        {   public void actionPerformed(ActionEvent e)
                {type = e.getActionCommand();}}
    
    public String getType() {
      return type;
    }
    ..
    ..
    
    
    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);    
      }
    }
    ..
    ..

Similar Threads

  1. NullPointerException by ActionListener
    By YouGina in forum AWT / Swing
    Replies: 8
    Last Post: 07-10-2009, 11:08 AM
  2. Determining ActionListener
    By siamino in forum New To Java
    Replies: 12
    Last Post: 05-25-2009, 11:04 PM
  3. Java actionlistener help
    By justsomeguy in forum AWT / Swing
    Replies: 1
    Last Post: 05-27-2008, 05:42 AM
  4. Demonstrating the ActionListener
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 04-23-2008, 08:20 PM
  5. ActionListener interface
    By tsantana in forum New To Java
    Replies: 2
    Last Post: 03-30-2008, 10:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •