0

I need some help, as I am quite the noob.

The program im trying to make here, used to work for my intentions, but as I tried to make my code more readable, I ran into a problem regarding ActionListener.

Before I made a new class to have all the methods in, I used button.addActionListener(this); and it worked just fine. Now that I wanted to put things in a separate class, I have absolutely no idea what to do.

So I guess my question is, how can I make ActionListener work in a situation like this, or am I just doing everything wrong here?

Here's the part of my code that I think is relevant(edited out most of it):

    //Class with frame, panels, labels, buttons, etc.

    class FemTreEnPlus {
        FemTreEnPlus() {
            //Components here!

            //Then to the part where I try to add these listeners
            cfg.addActionListener();
            Exit.addActionListener();
            New.addActionListener();
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
        public void run(){
        //Start the Program in the FemTreEnPlus Class
            new FemTreEnPlus();
        }     
    });  
}

That was the class with the frame, here's the other class, with the methods

public class FemTreEnMethods extends FemTreEnPlus implements ActionListener {

       //Perform Actions!
   public void actionPerformed(ActionEvent ae){  
       if(ae.getSource() == cfgButton){
        configureSettings();
       }
       if(ae.getSource() == newButton){
        newProject();
       }     
       if(ae.getSource() == exitButton){
        exitProgram();
       }
 }

   //All methods are down here

Thanks in advance for any help.

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720

1 Answers1

3

Despite the tutorials' examples show the use of listeners implemented in the way you do, IMHO is more useful use anonymous inner classes to implement listeners. For instance:

cfgButton.addActionListener(new ActionListener() {
    @Override
    public void actionPerfomed(ActionEvent e) {
        // do the stuff related to cfgButton here
    }
};

newButton.addActionListener(new ActionListener() {
    @Override
    public void actionPerfomed(ActionEvent e) {
        // do the stuff related to newButton here
    }
};

exitButton.addActionListener(new ActionListener() {
    @Override
    public void actionPerfomed(ActionEvent e) {
        // do the stuff related to exitButton here
    }
};

This approach has these advantages:

  • Listeners logic is well separated.
  • You don't need those nested if blocks asking who is the source of the event.
  • If you add a new button you don't have to modify your listener. Just add a new one.

Of course it depends on the case. If the behaviour will be the same for a set of components (for instance radio buttons or check boxes) then it makes sense have only one listener and use EventObject.getSource() to work with the event's source. This approach is suggested here and exemplified here. Note the examples also make use of anonymous inner classes in this way:

ActionListener actionListener = new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        // do something here
    }
};
Community
  • 1
  • 1
dic19
  • 17,821
  • 6
  • 40
  • 69
  • 1
    Although this answer is perfectly sufficient for this case, I would also try with actions. Here you've got a nice intro: http://docs.oracle.com/javase/tutorial/uiswing/misc/action.html It will make your solution more flexible - you can use one action for many UI components and controll its "enabled" state in one place ;) – radekEm Feb 28 '14 at 13:59
  • Thanks a ton! After messing around with a few errors here and there, I finally made it work the way you explained, inside a constructor(something I also have no idea what actually is). – user2900511 Feb 28 '14 at 14:02
  • @guitar_freak that's a good suggestion. I've never thought about use actions because I'm used to use anonymous action listeners but next time maybe I'll give it a try. – dic19 Feb 28 '14 at 14:07