Hello everyone,
I am trying to get an ActionListener to work in Java.

On lines 310 and 312 it says 'cannot find symbol: variable calcDisplayF'.

I am a complete noob when it comes to Event Handling so any help on this
subject would be greatly appreciated.

Here is the code (warning, it's long (315)):

/*
 * Creating here for development
 *
 * Please save all questions until the end of class...
 */
package macey;
/**
 * @author Mike
 */
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class normCalcDev implements ActionListener {

    public void normCalc() {
        // Content Pane
        JPanel content = new JPanel();
        content.setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
	JLabel spacingL = new JLabel(" ");
	c.gridy=1;
	c.gridx=0;
	c.gridwidth=1;
	c.gridheight=1;
	c.ipadx=0;
	c.ipady=0;
	c.insets=new Insets(0, 0, 0, 0);
	content.add(spacingL, c);
        // Contents
        JButton oneB = new JButton("1");
        c.gridy=2;
        c.gridx=0;
	c.gridwidth=1;
	c.gridheight=1;
	c.ipadx=0;
	c.ipady=0;
	c.insets=new Insets(0, 0, 5, 5);
        oneB.addActionListener(this);
        content.add(oneB, c);
        JButton twoB = new JButton("2");
        c.gridy=2;
        c.gridx=1;
	c.gridwidth=1;
	c.gridheight=1;
	c.ipadx=0;
	c.ipady=0;
	c.insets=new Insets(0, 0, 5, 5);
        content.add(twoB, c);
        JButton threeB = new JButton("3");
	c.gridy=2;
	c.gridx=2;
	c.gridwidth=1;
	c.gridheight=1;
	c.ipadx=0;
	c.ipady=0;
	c.insets=new Insets(0, 0, 5, 0);
	content.add(threeB, c);
        JButton fourB = new JButton("4");
	c.gridy=3;
	c.gridx=0;
	c.gridwidth=1;
	c.gridheight=1;
	c.ipadx=0;
	c.ipady=0;
	c.insets=new Insets(0, 0, 5, 5);
	content.add(fourB, c);
        JButton fiveB = new JButton("5");
	c.gridy=3;
	c.gridx=1;
	c.gridwidth=1;
	c.gridheight=1;
	c.ipadx=0;
	c.ipady=0;
	c.insets=new Insets(0, 0, 5, 5);
	content.add(fiveB, c);
        JButton sixB = new JButton("6");
	c.gridy=3;
	c.gridx=2;
	c.gridwidth=1;
	c.gridheight=1;
	c.ipadx=0;
	c.ipady=0;
	c.insets=new Insets(0, 0, 5, 0);
	content.add(sixB, c);
        JButton sevenB = new JButton("7");
	c.gridy=4;
	c.gridx=0;
	c.gridwidth=1;
	c.gridheight=1;
	c.ipadx=0;
	c.ipady=0;
	c.insets=new Insets(0, 0, 5, 5);
	content.add(sevenB, c);
        JButton eightB = new JButton("8");
	c.gridy=4;
	c.gridx=1;
	c.gridwidth=1;
	c.gridheight=1;
	c.ipadx=0;
	c.ipady=0;
	c.insets=new Insets(0, 0, 5, 5);
	content.add(eightB, c);
        JButton nineB = new JButton("9");
	c.gridy=4;
	c.gridx=2;
	c.gridwidth=1;
	c.gridheight=1;
	c.ipadx=0;
	c.ipady=0;
	c.insets=new Insets(0, 0, 5, 0);
	content.add(nineB, c);
        JButton plusB = new JButton("+");
	c.gridy=5;
	c.gridx=3;
	c.gridwidth=1;
	c.gridheight=1;
	c.ipadx=0;
	c.ipady=0;
	c.insets=new Insets(0, 0, 5, 0);
	content.add(plusB, c);
        JButton minusB = new JButton("-");
	c.gridy=4;
	c.gridx=3;
	c.gridwidth=1;
	c.gridheight=1;
	c.ipadx=3;
	c.ipady=0;
	c.insets=new Insets(0, 0, 5, 0);
	content.add(minusB, c);
        JButton multB = new JButton("*");
	c.gridy=2;
	c.gridx=3;
	c.gridwidth=1;
	c.gridheight=1;
	c.ipadx=2;
	c.ipady=0;
	c.insets=new Insets(0, 20, 5, 20);
	content.add(multB, c);
        JButton divB = new JButton("/");
	c.gridy=3;
	c.gridx=3;
	c.gridwidth=1;
	c.gridheight=1;
	c.ipadx=4;
	c.ipady=0;
	c.insets=new Insets(0, 0, 5, 0);
	content.add(divB, c);
        JButton expB = new JButton("^");
	c.gridy=4;
	c.gridx=8;
	c.gridwidth=1;
	c.gridheight=1;
	c.ipadx=29;
	c.ipady=0;
	c.insets=new Insets(0, 0, 5, 0);
	content.add(expB, c);
        JButton equalsB = new JButton("=");
	c.gridy=5;
	c.gridx=5;
	c.ipadx=16;
	c.gridwidth=1;
	c.gridheight=1;
	c.ipady=0;
	c.insets=new Insets(0, 0, 5, 20);
	content.add(equalsB, c);
        JButton cB = new JButton("C");
	c.gridy=4;
	c.gridx=7;
	c.gridwidth=1;
	c.gridheight=1;
	c.ipadx=7;
	c.ipady=0;
	c.insets=new Insets(0, 0, 5, 5);
	content.add(cB, c);
        JButton ceB = new JButton("CE");
	c.gridy=3;
	c.gridx=7;
	c.gridwidth=1;
	c.gridheight=1;
	c.ipadx=0;
	c.ipady=0;
	c.insets=new Insets(0, 0, 5, 5);
	content.add(ceB, c);
        JButton roundB = new JButton("Round");
	c.gridy=3;
	c.gridx=8;
	c.gridwidth=1;
	c.gridheight=1;
	c.ipadx=0;
	c.ipady=0;
	c.insets=new Insets(0, 0, 5, 0);
	content.add(roundB, c);
        JButton pointB = new JButton(".");
	c.gridy=5;
	c.gridx=2;
	c.gridwidth=1;
	c.gridheight=1;
	c.ipadx=4;
	c.ipady=0;
	c.insets=new Insets(0, 0, 5, 0);
	content.add(pointB, c);
        JButton sqrtB = new JButton("sqrt");
	c.gridy=2;
	c.gridx=5;
	c.gridwidth=1;
	c.gridheight=1;
	c.ipadx=0;
	c.ipady=0;
	c.insets=new Insets(0, 0, 5, 20);
	content.add(sqrtB, c);
        JButton modB = new JButton("%");
	c.gridy=4;
	c.gridx=5;
	c.ipadx=14;
	c.gridwidth=1;
	c.gridheight=1;
	c.ipady=0;
	c.insets=new Insets(0, 0, 5, 20);
	content.add(modB, c);
        JButton piB = new JButton("pi");
	c.gridy=3;
	c.gridx=5;
	c.ipadx=13;
	c.gridwidth=1;
	c.gridheight=1;
	c.ipady=0;
	c.insets=new Insets(0, 0, 5, 20);
	content.add(piB, c);
	JButton posnegB = new JButton("+/-");
	c.gridy=5;
	c.gridx=7;
	c.gridheight=1;
	c.ipadx=76;
	c.ipady=0;
	c.insets=new Insets(0, 0, 5, 0);
	c.gridwidth=2;
	content.add(posnegB, c);
	JButton zeroB = new JButton("0");
	c.gridy=5;
	c.gridx=0;
	c.gridwidth=2;
	c.gridheight=1;
	c.ipadx=47;
	c.ipady=0;
	c.insets=new Insets(0, 0, 5, 5);
	content.add(zeroB, c);
	JButton bkspB = new JButton("Backspace");
	c.gridy=2;
	c.gridx=7;
	c.gridwidth=2;
	c.gridheight=1;
	c.ipadx=26;
	c.ipady=0;
	c.insets=new Insets(0, 0, 5, 0);
	content.add(bkspB, c);
        JTextField calcDisplayF = new JTextField();
        calcDisplayF.setEnabled(false);
        c.gridy=0;
        c.gridx=0;
        c.gridwidth=10;
        c.ipadx=412;
	c.gridheight=1;
	c.ipady=0;
	c.insets=new Insets(0, 0, 0, 0);
        content.add(calcDisplayF, c);

        // Menubar
        JMenuBar menuBar = new JMenuBar();

        JMenu fileMenu = new JMenu("File");
        JMenuItem fileQuit = new JMenuItem("Quit");
        fileQuit.setMnemonic('Q');
        fileMenu.add(fileQuit);
        menuBar.add(fileMenu);

        JMenu optionsMenu = new JMenu("Options");
        JMenuItem piLength = new JMenuItem("pi Length");
        optionsMenu.add(piLength);
        menuBar.add(optionsMenu);

        JMenu helpMenu = new JMenu("Help");
        JMenuItem helpTutorial = new JMenuItem("Tutorial");
        helpTutorial.setMnemonic('T');
        helpMenu.add(helpTutorial);
        menuBar.add(helpMenu);

        // Main Window
        JFrame normCalcWin = new JFrame("Calculator");
        normCalcWin.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        normCalcWin.setSize(440, 260);
        normCalcWin.setLocation(425, 275);
        normCalcWin.setContentPane(content);
        normCalcWin.setJMenuBar(menuBar);
        normCalcWin.setResizable(false);
        normCalcWin.setVisible(true);
    }

    public void main(String[] args) {
        // Creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                normCalc();
            }
        });
    }

    public void actionPerformed(ActionEvent e) {
       if (e.getActionCommand().equals("1") != false) {
            calcDisplayF.setText("1");
        } else {
            calcDispalyF.setText("Other Button");
        }
    }
}

Recommended Answers

All 5 Replies

Your code should like this:

package macey;

/**
 * @author Mike
 */
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class NormCalcDev implements ActionListener {

    private JTextField calcDisplayF;

    public NormCalcDev() {
        // Content Pane
        ...
        calcDisplayF = new JTextField();
        // calcDisplayF.setEnabled(false);
        ...
    }

    public void actionPerformed(ActionEvent e) {

        if (e.getActionCommand().equals("1") != false) {
            calcDisplayF.setText("1");
        } else {
            calcDisplayF.setText("Other Button");
        }
    }

    public static void main(String[] args) {
        // Creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                new NormCalcDev();
            }
        });
    }
}

Line 36 create new instance of NormCalcDev class
Line 14 constructor of NormCalcDev class
Line 12,17 variable JTextField calcDisplayF need be declared outside the constructor to be visible
Line 31 need be static

Okay,
quuba, when I saved it like you said I get two errors:

Line 18: invalid method declaration; return type required
Line 313: non-static method normCalc() cannot be referenced from a static context.

Any ideas?

And thanks!

-WolfShield

What did I do?
Java is case sensitive
In the code I applied generally accepted standards:
I changed:

public class normCalcDev implements ActionListener {
//to
public class NormCalcDev implements ActionListener {

More about Java Naming Conventions http://java.about.com/od/javasyntax/a/nameconventions.htm
also I replaced method

public void normCalc() {
//with class constructor
    public NormCalcDev() {

and declared variable

private JTextField calcDisplayF;

in place, where calcDisplayF is visible inside whoole NormCalcDev class.
//
In my posted code normCalc() is absent.
Three dots (Line16, Line19) in my code means the absence of any change with the original.

Ah,
I'm sorry, that was my fault. I didn't think that the N needed
to be capitalized so I didn't capitalize it. My bad.

It now works!!

Thank you so much quuba!!

- WolfShield

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.