Results 1 to 7 of 7
  1. #1
    bigj is offline Member
    Join Date
    Dec 2009
    Posts
    68
    Rep Power
    0

    Default one inner class(an interface (ActionListener))) for multiple actions

    is it possible to write only one inner class(an interface) that will perfrom differenct tasks or listen to different events?

    for example:

    Java Code:
    MyComponent1.addActionListener(new MyAction());
    MyComponent2.addActionListener(new MyAction());
    MyComponent3.addActionListener(new MyAction());
    
    // the inner class 
    private MyAction implements ActionListener {
    
      public void actionPerformed(ActionEvent e) {
    
          // code that will perform different events from different components
       }
    }
    is this possible, if it is, where and how can i find the specific action/event that a components fires?


    I want to try this to lessen the length of my codes.. Thanks!
    Last edited by bigj; 01-31-2010 at 09:41 AM.

  2. #2
    bigj is offline Member
    Join Date
    Dec 2009
    Posts
    68
    Rep Power
    0

    Default

    SOLVED

    Java Code:
    MyComponent1.addActionListener(new MyAction());
    MyComponent2.addActionListener(new MyAction());
    MyComponent3.addActionListener(new MyAction());
    
    // the inner class 
    private MyAction implements ActionListener {
    
      public void actionPerformed(ActionEvent e) {
    
          if (MyComponent1.getActionCommand().equals(e.getActionCommand()))    {
                            // do the specific task
          }
    
          if (MyComponent2.getActionCommand().equals(e.getActionCommand()))    {
                            // do the specific task
          }
    
          if (MyComponent3.getActionCommand().equals(e.getActionCommand()))    {
                            // do the specific task
          }
       }
    }
    you also can use equalsIgnoreCase() to compare the string command for some
    safety(just in case).

    I dont know if this is a good way of handling an event/action.. any suggestions and comments will be accepted thank you! :)

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,022
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by bigj View Post
    I dont know if this is a good way of handling an event/action.. any suggestions and comments will be accepted thank you! :)
    I suggest a different listener for different events (or the same events fired by different sources) because otherwise your listener has to figure out what fired which event. This code may become more and more complex for more and more different events and/or sources of the event while the other scenario: "source of event --> specialized event handler" the listener immediately 'knows' what event was fired by what source because it was only registered for one event at one source object.

    kind regards,

    Jos

  4. #4
    bigj is offline Member
    Join Date
    Dec 2009
    Posts
    68
    Rep Power
    0

    Default

    how about this one sir..

    Java Code:
    private MyAction implements ActionListener {
    
      public void actionPerformed(ActionEvent e) {
    
          if (MyComponent1 == e.getSource())    {
                            // do the specific task
          }
    
          if (MyComponent2 == e.getSource())  {
                            // do the specific task
          }
    
          if (MyComponent3 == e.getSource())    {
                            // do the specific task
          }
       }
    }
    sorry for asking too much, I just want to avoid some errors or bugs before I type my codes,

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,022
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by bigj View Post
    how about this one sir..

    Java Code:
    private MyAction implements ActionListener {
    
      public void actionPerformed(ActionEvent e) {
    
          if (MyComponent1 == e.getSource())    {
                            // do the specific task
          }
    
          if (MyComponent2 == e.getSource())  {
                            // do the specific task
          }
    
          if (MyComponent3 == e.getSource())    {
                            // do the specific task
          }
       }
    }
    sorry for asking too much, I just want to avoid some errors or bugs before I type my codes,
    No need to apologize; try the same with separate ActionListeners and see which version you like most; the other version doesn't need all those if's because it 'knows' which object fired the event. It does need a bit of riff-raff around the actual code though in each different ActionListener implementation.

    kind regards,

    Jos

  6. #6
    bigj is offline Member
    Join Date
    Dec 2009
    Posts
    68
    Rep Power
    0

    Default

    thank you sir! :)

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    19

    Default

    Using separate listeners for each event (or each object) also breaks up the logic nicely. With the "all-in-one" technique you'll end up with completely unrelated logic all splurged into a single method, which is not good practice.

Similar Threads

  1. MouseListener / ActionListener in another class
    By newmanity in forum New To Java
    Replies: 9
    Last Post: 12-07-2009, 03:57 AM
  2. exception error in a ActionListener class
    By aserothbw in forum AWT / Swing
    Replies: 3
    Last Post: 09-22-2009, 12:59 PM
  3. exception error in a ActionListener class
    By aserothbw in forum AWT / Swing
    Replies: 6
    Last Post: 09-04-2009, 03:07 PM
  4. 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
  •