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 7041279
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:01:18+00:00 2026-05-28T02:01:18+00:00

I am trying to create a JMenuItem that is disabled by default, but a

  • 0

I am trying to create a JMenuItem that is disabled by default, but a method can be called to enable it. Just for the moment whilst I’m testing out my code, I want the method to be called when I click on another menu item. I have had a look at the documentation for JMenuItem, but I’m pretty new to Java and I’m having trouble finding exactly what I need. I’ve tried using the updateUI() command, but I that hasn’t worked, so I’m totally stuck. Thanks in advance for any help 🙂

This is what I have so far:

public class initialScreen extends JFrame implements ActionListener{

    Dimension screenSize = new Dimension(800,600);
    JMenuItem runE, newP;

    public initialScreen(){
        super("Experiment Control Suite");
        setSize(screenSize);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JMenuBar bar = new JMenuBar();

        JMenuItem newP = new JMenuItem("New");
        newP.addActionListener(this);

        runE = new JMenuItem("Run");
        runE.setEnabled(false);
        runE.addActionListener(this);

        JMenu exp = new JMenu("Experiment");
        exp.add(runE);      

        JMenu par = new JMenu("Participant");
        par.add(newP);
        bar.add(exp);
        bar.add(par);
        setJMenuBar(bar);

        setVisible(true);
    }

    public void enableRun(){
        runE.setEnabled(true);
        runE.updateUI();
    }

    public void actionPerformed(java.awt.event.ActionEvent e){
        if(e.getSource() == newP) {
            enableRun();  
        }
        else if(e.getSource() == runE) {
            System.out.println("run has been clicked");
        }
    }

}

  • 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-05-28T02:01:19+00:00Added an answer on May 28, 2026 at 2:01 am

    Your method enableRun is never invoked because of the following line:

    JMenuItem newP = new JMenuItem("New");
    

    Instead, refactor it as such,

    newP = new JMenuItem("New");
    

    Now, the field will be correctly initialized and registered as an ActionListener. And thus, when checking the source, enableRun will be invoked and the menu item will be enabled.

    Note that in this case, updateUI is completely unnecessary (I suggest you read the javadoc to learn its purpose).

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

Sidebar

Related Questions

I was trying to create a mouse clicked event of a JmenuItem but it
I'm trying create a bot which automatically likes Facebook posts. Using Mechanize I can
I am trying create a delegate representation of constructor by emitting a Dynamic Method,
Trying to create a macro which can be used for print debug messages when
Trying to create a generic video plugin for Expression Engine 2 where I can
Two questions. First question is I'm trying to create a simple form that when
I'm trying to create an android context menu (the one that pops-up when you
Hihi, I am trying to create a slide down menu using DIV, but hit
I am trying to create a Multi-Language WPF program that will load it's languages
I am trying to create a simple GUI. I have a menu bar that

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.