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

  • Home
  • SEARCH
  • 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 4623596
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T03:00:38+00:00 2026-05-22T03:00:38+00:00

Now, I have a JMenu, and some JMenuItems in it. I want my program

  • 0

Now, I have a JMenu, and some JMenuItems in it. I want my program to perform some action when JMenu’s and JMenuItem’s state is changed to “selected”. I don’t use MouseLitener’s MouseOver, because I want user to be able to navigate in menu using keyboards too. Now, I wrote this listener:

class MenuItemListener implements ChangeListener {
    @Override
    public void stateChanged(ChangeEvent arg0) {
        JMenuItem item = (JMenuItem) arg0.getSource();
        if(item.isSelected())
            System.out.println(item.getText()+" pressed!");
    }
}

When I add this listener to JMenu, it works properly, but when I add it to JMenuItem, nothing happens… When I delete if statement so that listener reacts both, when menu is selected and diselected I works fine for JMenu as well as for JMenuItem. So, as I see, JMenuItem can’t “pass” isSelected() test… But what can be a problem? :S

  • 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-22T03:00:39+00:00Added an answer on May 22, 2026 at 3:00 am

    No offense intended in any direction, this is just one of those questions with a history

    • initial requirement: do-something when a mouse is over JMenuItem
    • initial everybody’s darling: MouseListener
    • initial deviating suggestion (kudos to @mKorbel!): ChangeListener on the buttonModel, checking the rollover property

    • refined requirement: doSomething when JMenuItem just highlighted, by both keyboard and mouse over.

    • refined darling: ChangeListener on the buttonModel, property not specified
    • refined deviation: ActionListener

    • current requirement: doSomething when JMenu or JMenuItem “selected” property changed.

    • current darling: can’t be done with a listener, override …
    • current deviations: Action, MenuListener …

    The correct and complete (in hindsight, though, as the keyboard wasn’t yet mentioned) answer was available in the first round already: some semantic listener which is “low-level enough” to capture state changes (candidates are rollover, armed, selected, pressed on the buttonModel level) which make the menuItems change their highlighted state. Unfortunately, the exact relation is not well known (to me, at least), undocumented (read: lazy me couldn’t find anything on a quick look) and even confusing (again, to me) as rollover is false always (?) for menuItems

    The experimentalist’s reaction is to .. try: below is a code snippet which listens and logs the state changes on some menu tree (simply throw into an arbitrary menuBar and move the mouse around and navigate by keyboard).

    And the winner is:
    – use a ChangeListener and check if the source is either selected or armed.

        ChangeListener ch = new ChangeListener() {
    
            @Override
            public void stateChanged(ChangeEvent e) {
                if (e.getSource() instanceof JMenuItem) {
                    JMenuItem item = (JMenuItem) e.getSource();
                    if (item.isSelected() || item.isArmed()) {
                        System.out.println("Highlighted: " + item.getActionCommand());
                    }
                }
            }
        };
    

    works for both keyboard and mouse, both JMenu and JMenuItem

    //----------- code snippet to track property changes in menuItem/buttonModel
    
        // test menu
        JMenu menu = new JMenu("Sample menu");
        menu.setMnemonic('s');
        installListeners(menu);
    
        // first menuitem
        JMenuItem other = menu.add("content1");
        installListeners(other);
        // second menuitem
        other = menu.add("again + ");
        installListeners(other);
    
        // sub
        JMenu sub = new JMenu("subMenu");
        installListeners(sub);
        menu.add(sub);
    
        // menus in sub
        other = sub.add("first in sub");
        installListeners(other);
        other = sub.add("second in sub");
        installListeners(other);
    
        getJMenuBar().add(menu);
    
    private void installListeners(JMenuItem menu) {
        menu.getModel().addChangeListener(getChangeListener());
        menu.addChangeListener(getChangeListener());
    }
    
    private ChangeListener getChangeListener() {
        ChangeListener ch = new ChangeListener() {
    
            @Override
            public void stateChanged(ChangeEvent e) {
                if (e.getSource() instanceof ButtonModel) {
                    ButtonModel model = (ButtonModel) e.getSource();
                    System.out.println("from model: " + createStateText(model));
                } else if (e.getSource() instanceof JMenuItem) {
                    JMenuItem item = (JMenuItem) e.getSource();
                    System.out.println("  from item: " + createStateText(item));
                }
            }
    
            private String createStateText(ButtonModel model) {
                String text = model.getActionCommand() + " armed: " + model.isArmed();
                text += " selected: " + model.isSelected();
                text += " rollover " + model.isRollover();
                text += " pressed: " + model.isPressed();
                return text;
            }
    
            private String createStateText(JMenuItem model) {
                String text = model.getActionCommand() + " armed: " + model.isArmed();
                text += " selected: " + model.isSelected();
                // not supported on JMenuItem nor on AbstractButton
               // text += " rollover " + model.isRollover();
               // text += " pressed: " + model.isPressed();
                return text;
            }
        };
        return ch;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I now have a running Java program which only lacks of the final step,that
I now have this code based on some of the answers below. Is this
I now have a project which I want do profiling on, but it used
I now have a new Desfire EV1 card. And I want to set PICC
I now have the following code, which calls a WCF service, it get some
I now have some functions disabled. In PHPInfo, I can see these functions are
I now have my application on Symfony 1.4 on the server. I use FileZilla
I now have to refactor some code, it's basically one method(its around 1000 lines
I'm trying to build a particular JMenu . I want a JMenuItem with JMenu
You now have to pay to use the google translate api. I'm happy to

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.