Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8239851
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T20:17:12+00:00 2026-06-07T20:17:12+00:00

Why on the first line of class actionPerformed(), can I not use the protected

  • 0

Why on the first line of class actionPerformed(), can I not use the protected JMenuItem menuItem; variable which I declared at the top of the MainMenuBar() class?

The 2nd line is the one I commented out, and it works.

But, I want to be able to use one JMenuItem object name across classes without having to reinstantiate it.

How can I reuse menuItem here correctly?

Thank you.

package base;

import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JTextArea;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

public class MainMenuBar implements ActionListener {
    protected JMenuBar menuBar;
    protected JMenu menu;
    protected JMenuItem menuItem;

    JTextArea output;
    String newline = "\n";

    public JMenuBar getMenuBar() {
        menuBar = new JMenuBar();

        // 1st Menu
        menu = new JMenu("File");
        menu.setMnemonic(KeyEvent.VK_F);

        menuItem = new JMenuItem("New Note");
        menuItem.setMnemonic(KeyEvent.VK_N);

        menu.add(menuItem);
        menu.addSeparator();

        menuItem = new JMenuItem("Exit");
        menuItem.setMnemonic(KeyEvent.VK_X);

        menu.addActionListener(this);
        menu.add(menuItem);

        menuBar.add(menu);

        // 2nd Menu
        menu = new JMenu("Help");
        menu.setMnemonic(KeyEvent.VK_H);

        menuItem = new JMenuItem("About Java Sticky Notes");
        menuItem.setMnemonic(KeyEvent.VK_A);

        menu.addActionListener(this);
        menu.add(menuItem);

        menuBar.add(menu);

        return menuBar;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        menuItem source = new JMenuItem("MainMenuBar.java");
        //JMenuItem source = (JMenuItem) (e.getSource());
        String s = "Action event detected." + newline + "    Event source: "
                + source.getText() + " (an instance of " + getClassName(source)
                + ")";
        output.append(s + newline);
        output.setCaretPosition(output.getDocument().getLength());
    }

    // Returns just the class name -- no package info.
    protected String getClassName(Object o) {
        String classString = o.getClass().getName();
        int dotIndex = classString.lastIndexOf(".");
        return classString.substring(dotIndex + 1);
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T20:17:14+00:00Added an answer on June 7, 2026 at 8:17 pm

    You have a syntax error, compiler assumes menuItem is a class, since it is followed by source. Change the following:

    menuItem source = new JMenuItem("MainMenuBar.java");
    

    to:

    menuItem = new JMenuItem("MainMenuBar.java");
    

    But then you have to take care of the source, since it is used in the following lines.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I parse my CSV file without parsing first line ? This class
Why does the first line work but not the second: Class<? extends Class> c1
I'm trying to select the first line item ( <li> ) with the class
I currently use a combination of LVL and Proguard as a first line of
In an AS3 project, we can access loaderInfo in the first line of the
I have a text file which has first line as below: j0W82LBrSdUbw Basically it
Can I somehow omit the first line? i.e. $this->load->model('Model_name'); and autoload it when necessary?
Does anyone know why you can reference a static method in the first line
The first line of bin/jekyll of the Jekyll gem uses the following syntax: $:.unshift
I'm printing the first line from a file: with open(path,r,encoding='utf8') as f: for i,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.