0

I fixed the error, but I wanted to ask another way for fixing this error. Tried to make a basic calculator and now I'm working with listeners. When I try to call a button, it says that it cannot make a static reference to a nonstatic field. Here's main class code:

import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class CalcComb {
    JPanel windowContent;
    JFormattedTextField displayField;
    static JButton button0;
    static JButton button1;
    static JButton button2;
    static JButton button3;
    static JButton button4;
    static JButton button5;
    static JButton button6;
    static JButton button7;
    static JButton button8;
    static JButton button9;
    static JButton buttonPoint;
    static JButton buttonEqual;
    static JButton buttonplus;
    static JButton buttonminus;
    static JButton buttonclear;
    JPanel p1;
    JPanel p2;

public CalcComb() {
    windowContent = new JPanel();
    BorderLayout bl = new BorderLayout();
    windowContent.setLayout(bl);
    displayField = new JFormattedTextField(0);
    displayField.setHorizontalAlignment(SwingConstants.RIGHT);
    windowContent.add("North", displayField);
    button0=new JButton("0");
    button1=new JButton("1");
    button2=new JButton("2");
    button3=new JButton("3");
    button4=new JButton("4");
    button5=new JButton("5");
    button6=new JButton("6");
    button7=new JButton("7");
    button8=new JButton("8");
    button9=new JButton("9");
    buttonplus=new JButton("+");
    buttonminus=new JButton("-");
    buttonclear=new JButton("C");
    buttonPoint = new JButton(".");
    buttonEqual=new JButton("=");
    p1 = new JPanel();
    p2 = new JPanel();
    GridLayout gl2 = new GridLayout(3, 1);
    GridLayout gl = new GridLayout(3, 3);
    p1.setLayout(gl);
    p2.setLayout(gl2);
    p1.add(button1);
    p1.add(button2);
    p1.add(button3);
    p1.add(button4);
    p1.add(button5);
    p1.add(button6);
    p1.add(button7);
    p1.add(button8);
    p1.add(button9);
    p1.add(button0);
    p1.add(buttonPoint);
    p1.add(buttonEqual);
    p2.add(buttonplus);
    p2.add(buttonminus);
    p2.add(buttonclear);
    windowContent.add("West", p2);
    windowContent.add("Center", p1);
    JFrame frame =  new JFrame("Calculator");
    frame.setSize(400, 400);
    frame.setContentPane(windowContent);
    frame.pack();
    frame.setVisible(true);
    CalcEngine engine = new CalcEngine();
    button0.addActionListener(engine);
    button1.addActionListener(engine);
    button2.addActionListener(engine);
    button3.addActionListener(engine);
    button4.addActionListener(engine);
    button5.addActionListener(engine);
    button6.addActionListener(engine);
    button7.addActionListener(engine);
    button8.addActionListener(engine);
    button9.addActionListener(engine);
    buttonplus.addActionListener(engine);
}
public static void main(String[] args) {
    CalcComb calc = new CalcComb();
}
}

Here's my listener class code:

import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class CalcEngine implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        Object src = e.getSource();
        JButton clickedButton = (JButton) e.getSource();
        String clickedButtonLabel = clickedButton.getText();
        if(e.getSource() instanceof JButton) {
            JOptionPane.showConfirmDialog(null, "You pressed " + clickedButtonLabel, "Just a test", JOptionPane.PLAIN_MESSAGE);
            if (clickedButton == CalcComb.buttonplus) {
                JOptionPane.showConfirmDialog(null, "Congratulations", "Just one more test", JOptionPane.PLAIN_MESSAGE);
            }
        }
    }
}

Any ideas to fix that without a word static before each JButton?

2 Answers2

3

You should not use the static keyword to access the buttons in CalcComb class. You must use setActionCommand() method for your JButtons to set a unique string for each of the buttons. Then in the actionPerformed() you can use getActionCommand() to get that unique string you've set before and determine which button is pressed. No need to use static references in CalcComb class:

ActionListener actionListener = new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        switch (e.getActionCommand()) {
        case "PLUS":
            // DO SOMTHING:
            break;
        case "MULT":
            // DO SOMTHING:
            break;
        default:
            break;
        } 
    }
};

JButton b = new JButton("+");
b.setActionCommand("PLUS");
b.addActionListener(actionListener);

JButton b1 = new JButton("*");
b1.setActionCommand("MULT");
b1.addActionListener(actionListener);

Also you may want to avoid the defining ActionListener implementation inside of the init() method or your constructor. A good practice is that your CalcComb class implement the ActionListener intreface, and in this way you have the actionPerformed method inside of the CalcComb class, but anyway using setActionCommand() and getActionCommand() methods is better than checking the reference checking with the member variables of your class:

public class CalcComb implements ActionListener{
...
...


    @Override
    public void actionPerformed(ActionEvent e) {
        switch (e.getActionCommand()) {
        case "PLUS":
            // DO SOMETHING:
            break;
        case "MULT":
            // DO SOMETHING:
            break;
        default:
            break;
        } 
    }
}

Good Luck.

STaefi
  • 4,297
  • 1
  • 25
  • 43
0

You can use below process:

 if (clickedButton == new CalcComb().buttonplus) {
            JOptionPane.showConfirmDialog(null, "Congratulations", "Just one  more test", JOptionPane.PLAIN_MESSAGE);
        }
Rafiq
  • 740
  • 1
  • 5
  • 17
  • I think clickedButton == new CalcComb().buttonplus would not be true because every time an action happens you are making a new instance of CalcComb class and the == operator can not match the reference of the buttonplus with source of the actionEvent. – STaefi Jul 26 '15 at 10:55