"cannot find symbol" on menuItem for ActionListener

Discussion in 'Java' started by justineee, Oct 24, 2008.

  1. justineee

    justineee Guest

    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
    justineee, Oct 24, 2008
    #1
    1. Advertising

  2. justineee

    Mark Space Guest

    justineee wrote:
    >
    > 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.
    Mark Space, Oct 24, 2008
    #2
    1. Advertising

  3. justineee

    justineee Guest

    On Oct 24, 9:04 am, Mark Space <> wrote:
    > justineeewrote:
    >
    > > 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.



    when i do that, the item is not in the interface already.. It can't be
    seen.. thanks for the reply anyway :)
    justineee, Oct 24, 2008
    #3
  4. In article
    <>,
    justineee <> wrote:

    > On Oct 24, 9:04 am, Mark Space <> wrote:
    > > justineeewrote:
    > >
    > > > 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.

    >
    >
    > [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.

    --
    John B. Matthews
    trashgod at gmail dot com
    http://home.roadrunner.com/~jbmatthews/
    John B. Matthews, Oct 24, 2008
    #4
  5. justineee

    justineee Guest

    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. :)
    justineee, Oct 24, 2008
    #5
  6. Peter Duniho wrote:
    >
    > 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..
    Mike Schilling, Oct 25, 2008
    #6
  7. Peter Duniho wrote:
    > On Sat, 25 Oct 2008 00:10:34 -0700, Mike Schilling
    > <> wrote:
    >
    >> Peter Duniho wrote:
    >>>
    >>> 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..

    >
    > An instance method such as a constructor. :)


    Or an init block. (I think that's all the cases.)
    Mike Schilling, Oct 25, 2008
    #7
  8. justineee

    Guest

    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);
    }
    }
    On Thursday, October 23, 2008 5:57:46 PM UTC-7, justineee wrote:
    > 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
    , Dec 30, 2013
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Danny
    Replies:
    1
    Views:
    4,667
    Andrew Thompson
    May 26, 2004
  2. Replies:
    5
    Views:
    6,532
    Tilman Bohn
    Dec 16, 2004
  3. luch
    Replies:
    5
    Views:
    6,339
  4. fybar

    cannot find symbol

    fybar, Apr 25, 2006, in forum: Java
    Replies:
    6
    Views:
    9,231
    Oliver Wong
    Apr 25, 2006
  5. Replies:
    6
    Views:
    1,736
Loading...

Share This Page