vendredi 3 février 2017

Replace many "else if" with something else

I am using an ActionListener and have lots of else if statements in order to know which button is pressed and run some code depending on the button.

Is there a way to make the code nicer? I have nearly 10 else if statements following each other, is there something else I could use instead?

Sample of code:

class BtnListener implements ActionListener {   
            @Override
        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == menu.getOpen()) {
                getFile();
            } else if (e.getSource() == btnPlay) {

            } else if (e.getSource() == btnQuit)) {
         }
}

Thanks.

Aucun commentaire:

Enregistrer un commentaire