"cannot find symbol" on menuItem for ActionListener

J

justineee

Hi again everyone,

I am quite new to java and this is what I have with my project..
My problem is.. the compiler cannot find menu item "exitItem" in the
actionPerformed method..

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

public class MainFrame extends JPanel implements ActionListener {
private JButton displayButton;
private JButton findButton;
private JButton addButton;
private JTextArea displayWords;
private JMenuBar menuBar;
private JTextField wordInput;
private JTextField inputWord;
private JLabel mpLabel;
private JButton deleteButton;

public MainFrame()
{
JMenu fileMenu = new JMenu ("File");
JMenuItem exitItem = new JMenuItem ("Exit");
JMenuItem loadItem = new JMenuItem ("Load");
JMenu configureMenu = new JMenu ("Configure");
JMenuItem setwordsItem = new JMenuItem ("Set number of
words");
JMenu helpMenu = new JMenu ("Help");
JMenuItem contentsItem = new JMenuItem ("Contents");
JMenuItem aboutItem = new JMenuItem ("About");

fileMenu.add (loadItem);
fileMenu.add (exitItem);
configureMenu.add (setwordsItem);
helpMenu.add (aboutItem);
helpMenu.add (contentsItem);

displayButton = new JButton ("Display Words");
findButton = new JButton ("Find");
addButton = new JButton ("Add");
displayWords = new JTextArea (5, 5);
menuBar = new JMenuBar();
menuBar.add (fileMenu);
menuBar.add (configureMenu);
menuBar.add (helpMenu);
wordInput = new JTextField (6);
inputWord = new JTextField (5);
mpLabel = new JLabel ("E-Dictionary v1.0");
deleteButton = new JButton ("Delete");

displayButton.setToolTipText ("Display all words");
findButton.setToolTipText ("Find a word");
addButton.setToolTipText ("Add a word");
deleteButton.setToolTipText ("Delete a word");


setPreferredSize (new Dimension (281, 460));
setLayout (null);

add (displayButton);
add (findButton);
add (addButton);
add (displayWords);
add (menuBar);
add (wordInput);
add (inputWord);
add (mpLabel);
add (deleteButton);

displayButton.addActionListener(this);
findButton.addActionListener(this);
addButton.addActionListener(this);
deleteButton.addActionListener(this);
exitItem.addActionListener(this);
setwordsItem.addActionListener(this);

fileMenu.setMnemonic('F');
exitItem.setMnemonic('X');
configureMenu.setMnemonic('C');
helpMenu.setMnemonic('H');
loadItem.setMnemonic('L');


displayButton.setBounds (10, 400, 145, 30);
findButton.setBounds (175, 40, 95, 25);
addButton.setBounds (10, 365, 70, 25);
displayWords.setBounds (10, 75, 260, 280);
menuBar.setBounds (0, 0, 705, 25);
wordInput.setBounds (90, 365, 180, 25);
inputWord.setBounds (10, 40, 155, 25);
mpLabel.setBounds (5, 440, 135, 25);
deleteButton.setBounds (165, 400, 110, 30);

displayWords.setEditable(false);
}

public void actionPerformed (ActionEvent jnd)
{
if(jnd.getSource() == exitItem)
{
System.exit(0);
}
}

}



Can anyone pls give me some tips?
Thanks

Justine
 
M

Mark Space

justineee said:
public class MainFrame extends JPanel implements ActionListener {
public MainFrame()
{
JMenu fileMenu = new JMenu ("File");
JMenuItem exitItem = new JMenuItem ("Exit");
}

public void actionPerformed (ActionEvent jnd)
{
if(jnd.getSource() == exitItem)
Can anyone pls give me some tips?


Unless I miss my guess, exitItem is a local variable in the constructor,
which is not in scope in actionPerfromed. You'll need to move exitItem
to class scope so both methods can see it.
 
J

justineee

justineeewrote:




Unless I miss my guess, exitItem is a local variable in the constructor,
which is not in scope in actionPerfromed.  You'll need to move exitItem
to class scope so both methods can see it.


when i do that, the item is not in the interface already.. It can't be
seen.. thanks for the reply anyway :)
 
J

John B. Matthews

justineee said:
justineeewrote:




Unless I miss my guess, exitItem is a local variable in the constructor,
which is not in scope in actionPerfromed.  You'll need to move exitItem
to class scope so both methods can see it.


[W]hen do that, the item is not in the interface already.


What interface?
It can't be seen. [T]hanks for the reply anyway. :)

Mark Space has identified the error precisely, and his proposal corrects
your problem. I have added an appropriate main method to your code:

....
private JButton deleteButton;
private JMenuItem exitItem;

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame f = new JFrame();
MainFrame panel = new MainFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(panel);
f.pack();
f.setVisible(true);
}
});
}

public MainFrame() {
JMenu fileMenu = new JMenu ("File");
exitItem = new JMenuItem ("Exit");
....

You do yourself and your users a disservice by not using a suitable
layout manager.
 
J

justineee

Thanks to all who replied.. the one I'm pointing out is that when I
move the declaration inside the method.. the item disappears to the
interface (Jframe).

Anyway, Thanks all. I did it with your tips. :)
 
M

Mike Schilling

Peter said:
As far as I know, there's no way to declare a _static_ nested
anonymous class. I think that anonymous classes are always inner
classes.

Anonymous classes defined in static methods are effectively static
(that is, they have no parent instance), though they're still called
"inner classes" You're right that there's no way to declare that an
anonymous class defined in an instance method won't access its parent
instance..
 
S

suku260

When yall do that, in order to call the objects into an action listener, initially set the objects as public before the main. When doing this, there is no need to instantiate the variables.


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

public class MainFrame extends JPanel implements ActionListener {
private JButton displayButton;
private JButton findButton;
private JButton addButton;
public JTextArea displayWords;
public JMenuBar menuBar;
public JTextField wordInput;
public JTextField inputWord;
public JLabel mpLabel;
public JButton deleteButton;
public JMenu fileMenu;
public JMenuItem exitItem;
public JMenuItem loadItem
public JMenu configureMenu;
public JMenuItem setwordsItem;
public JMenu helpMenu;
JMenuItem contentsItem
public MainFrame()
{
fileMenu = new JMenu ("File");
exitItem = new JMenuItem ("Exit");
loadItem = new JMenuItem ("Load");
configureMenu = new JMenu ("Configure");
setwordsItem = new JMenuItem ("Set number of
words");
helpMenu = new JMenu ("Help");
JMenuItem contentsItem = new JMenuItem ("Contents");
JMenuItem aboutItem = new JMenuItem ("About");

fileMenu.add (loadItem);
fileMenu.add (exitItem);
configureMenu.add (setwordsItem);
helpMenu.add (aboutItem);
helpMenu.add (contentsItem);

displayButton = new JButton ("Display Words");
findButton = new JButton ("Find");
addButton = new JButton ("Add");
displayWords = new JTextArea (5, 5);
menuBar = new JMenuBar();
menuBar.add (fileMenu);
menuBar.add (configureMenu);
menuBar.add (helpMenu);
wordInput = new JTextField (6);
inputWord = new JTextField (5);
mpLabel = new JLabel ("E-Dictionary v1.0");
deleteButton = new JButton ("Delete");

displayButton.setToolTipText ("Display all words");
findButton.setToolTipText ("Find a word");
addButton.setToolTipText ("Add a word");
deleteButton.setToolTipText ("Delete a word");


setPreferredSize (new Dimension (281, 460));
setLayout (null);

add (displayButton);
add (findButton);
add (addButton);
add (displayWords);
add (menuBar);
add (wordInput);
add (inputWord);
add (mpLabel);
add (deleteButton);

displayButton.addActionListener(this);
findButton.addActionListener(this);
addButton.addActionListener(this);
deleteButton.addActionListener(this);
exitItem.addActionListener(this);
setwordsItem.addActionListener(this);

fileMenu.setMnemonic('F');
exitItem.setMnemonic('X');
configureMenu.setMnemonic('C');
helpMenu.setMnemonic('H');
loadItem.setMnemonic('L');


displayButton.setBounds (10, 400, 145, 30);
findButton.setBounds (175, 40, 95, 25);
addButton.setBounds (10, 365, 70, 25);
displayWords.setBounds (10, 75, 260, 280);
menuBar.setBounds (0, 0, 705, 25);
wordInput.setBounds (90, 365, 180, 25);
inputWord.setBounds (10, 40, 155, 25);
mpLabel.setBounds (5, 440, 135, 25);
deleteButton.setBounds (165, 400, 110, 30);

displayWords.setEditable(false);
}

public void actionPerformed (ActionEvent jnd)
{
if(jnd.getSource() == exitItem)
{
System.exit(0);
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,058
Messages
2,570,446
Members
47,118
Latest member
saliar khanbaba

Latest Threads

Top