Hey all I am a month or two in coding in java and currently I am doing a java calculator gui project, my professor requires us to code the gui by hand, and I did so with a panel and gridbaglayout layout manager, anyways since this is a calculator I have a plethora of buttons to listen to during program execution, I ask how do I perform tasks accordingly with each button?

Do I create an actionlistener class or mouselistener class? and with each button I have, do I assign a unique class for each button component I have? i.e: one class for the button 1, one class for the addition addition button and so on.

Or can I just, (as I have been planning earlier) create one action or mouselistener class and override the existing actionPerformed method in the original class to be implemented and use it for the whole of my buttons.

I do not however know how to perform the latter approach. Although, I have this vague idea that my actionPerformed method will have like, a case statement that reads the currently pressed button, although I do not know how to detect the currently pressed button.

A little background on my code: I have stored all digit buttons (i.e 0 - 9) in an array list of buttons for easy access, I have also stored all calculator functions in a nother arraylist of buttons with their own equivalent identifier in an enum I have created.

That is all, thank you for the help and for reading this lengthy post.

Use action listener for each unique button category. For example, just a simple idea is to make a action listener for the numbers and have a actin listener for the functions. Here is a non-compiled on the spot simple example :

class Calculator extends JPanel{
 private ArrayList<JButton> numbers;
 private ArrayList<JButton> functions;
 private String currentExpression; //current expression to calculate
 public Calculator(){
    CalculatorNumberButtonListener bl = new CalculatorNumberButtonListener();
    for(JButton b : numbers) b.addActionListener( bl );
 }
 //...stuff
 private class CalculatorNumberButtonListener implements ActionListener{
  public void actionPerformed(ActionEvent event){
    //error check
    //convert number or add it to currenExpression do something
  }
 }
 private class CalculatorFunctionButtonListener implements ActionListener{
  public void actionPerformed(ActionEvent event){
    //error check
    //evaluate or do something
  }
 }
};

To check if whether what value or function is to be used during the actionPerformed method I use if statements that reads the text of the button right? This is just a hunch though

here is my try on the calculator number button listener, I know that this is still not yet correct for I can't detect what button was pressed, what am I still missing?

class NumberButtonClickListener implements ActionListener {
           JButton currentButtonPressed;// = btn_numberButtonList.get(0);

           public void actionPerformed (ActionEvent clickEvent) {
               if ((tbox_equationField.getText().length() >= 1) && 
                       !(tbox_equationField.getText().equals("0"))) {
                   tbox_equationField.setText(tbox_equationField.getText().concat("Value Clicked"));
               }
               
               else if ((tbox_equationField.getText().length() == 1) && 
                       (tbox_equationField.getText().equals("0"))) {
                   tbox_equationField.setText("Value Clicked");
               } 

           }
       }

I am currently doing the function button listener too, the logic is kinda tying me up that's why I haven't posted it yet.

Re-did my code into something like this:

class GenericButtonListener implements ActionListener {
            
            JButton currentButtonPressed;
            
            public void actionPerformed (ActionEvent clickEvent) { }
            
            public void setSourceButton(ActionEvent clickEvent, ArrayList<JButton> listOfButtons) {
               for (int i = 0; i < listOfButtons.size(); i++) {
                   if (clickEvent.getSource().equals(listOfButtons.get(i))) {
                       currentButtonPressed = listOfButtons.get(i);
                       break;
                   }
               }
            }
            
            public JButton getSourceButton () {
                return currentButtonPressed;
            }
            
        }

        class NumberButtonClickListener extends GenericButtonListener {

            @Override
           public void actionPerformed (ActionEvent clickEvent) {
               setSourceButton(clickEvent, btn_numberButtonList);
               if ((tbox_equationField.getText().length() >= 1) &&
                       !(tbox_equationField.getText().equals("0"))) {
                   tbox_equationField.setText(tbox_equationField.getText().concat(getSourceButton().getText().trim()));
               }

               else if ((tbox_equationField.getText().length() == 1) &&
                       (tbox_equationField.getText().equals("0"))) {
                   tbox_equationField.setText(currentButtonPressed.getText().trim());
               }

           }
       }

currently, recoding my operation button handlers, suggestions will be greatly appreciated for I know something is wrong with my code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.