1 Reply Latest reply: Nov 11, 2013 6:00 PM by TPD-Opitz RSS

    Help me fix this even listener issue please

    Hotmax

      i created a private inner class that implements an Action Listener. Thee is an ActionPerform method inside of it.

       

       

      so how could i write the code insode that action performed method to fire when the buton is clicked....thisis the code:

       

       

      public class ChaseMainWindow  extends ChaseGui implements ActionListener{

          OptionListener OptionListener = new OptionListener();

          public ChaseMainWindow(){

       

           

           JPanel Option = new JPanel( new GridLayout(4,0));

           JButton BtnBal = new JButton("BALANCE INQUIRY");

           BtnBal.setBackground(Color.white );

           JButton BtnDep = new JButton("MAKE A DEPOSIT");

           BtnDep.setBackground(Color.white );

           JButton BtnWith = new JButton("WITHDRAW MONEY");

           BtnWith.setBackground(Color.white );

           JButton BtnExit = new JButton("EXIT");

           BtnExit.setBackground(Color.white );

           Option.add(BtnBal);

           Option.add(BtnDep);

           Option.add(BtnWith);

           Option.add(BtnExit);

            

           add(Option);

           BtnBal.addActionListener(OptionListener);

           BtnDep.addActionListener(OptionListener);

           BtnWith.addActionListener(OptionListener);

           BtnExit.addActionListener(OptionListener);

          }

           private class OptionListener implements ActionListener{

           @Override

          public void actionPerformed(ActionEvent e){

         try { 

            

                if (e.getSource() == BtnBal) {

        

                     // Stuff I want to happen

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

            

                     // Other Stuff

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

                     ;

                     // Other Stuff

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

                           

                     // Other Stuff

                } else {     //third button

                     // More stuff

                }

           } catch (NumberFormatException nfe) {

                // Null argument in keyPressed triggers this

                // catches empty string exception from TextBox

           }

      }

          public void OnBtnBalPressed(){

          //Code goes here

          }

         

      public void OnBtnDepPressed(){

              //Code goes here

          }

         

      public void OnBtnWithPressed(){

              //Code goes here

          }

         

      public void OnBtnExitPressed(){

              //Code goes here

          }

      }

      }

        • 1. Re: Help me fix this even listener issue please
          TPD-Opitz

          first of all please get used to standard java naming conventions (at least when posting to this forum). Names of references (aka variables) and methods start lower case...

           

          next: how do you know that your ActionListener is not executed?

           

          and last but not least: ist a good practice to spend each button its own ActionListener implementation. This vanishes the need  for the if-else if cascade...

           

          bye

          TPD