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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T23:34:25+00:00 2026-06-12T23:34:25+00:00

I have a user request to add an accelerator to a sub menu (JMenu)

  • 0

I have a user request to add an accelerator to a sub menu (JMenu) which would allow the user to press the short cut and have the corresponding sub menu “fold out”, showing its contained menu items.

I don’t recall every having seen something like this (either in Java or any other language). Our application is written in Java using Swing. We have a number of JMenuItems with accelerators that work well, but when I attempted to add an accelerator to JMenu I get the following exception:

java.lang.Error: setAccelerator() is not defined for JMenu. Use setMnemonic() instead.

I’ve tried to use the MenuDemo! code to experiment with this a bit further.

This is what I tried:

//a submenu
menu.addSeparator();
submenu = new JMenu("A submenu");
submenu.setMnemonic(KeyEvent.VK_S);
submenu.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_U, InputEvent.CTRL_MASK));

The last line is the one added by me, which causes the exception.

I’ve tried extensive googling but all I can find is articles on how to add accelerators to JMenuItem.

It seems the JMenu does not support this natively. Is there any workaround to achieve this behaviour?

  • 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-12T23:34:26+00:00Added an answer on June 12, 2026 at 11:34 pm

    Another option is to override the accelerator get/set and reproduce the JMenuItem behaviour. Then the UI will do the rest of the job.

    The important thing is to fire the property change and have a consistent get/set for the accelerator. The advantage of this solution is that it also provides a visual indication of the shortcut/accelerator.

    Here is a small demo code:

    import java.awt.event.KeyEvent;
    
    import javax.swing.JFrame;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    import javax.swing.KeyStroke;
    import javax.swing.SwingUtilities;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    
    public class TestMenu {
    
        protected void initUI() {
            JFrame frame = new JFrame(TestMenu.class.getSimpleName());
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            JMenuBar bar = new JMenuBar();
            JMenu topMenu = new JMenu("Top Menu");
            JMenu subMenu = new JMenu("Sub menu") {
                private KeyStroke accelerator;
    
                @Override
                public KeyStroke getAccelerator() {
                    return accelerator;
                }
    
                @Override
                public void setAccelerator(KeyStroke keyStroke) {
                    KeyStroke oldAccelerator = accelerator;
                    this.accelerator = keyStroke;
                    repaint();
                    revalidate();
                    firePropertyChange("accelerator", oldAccelerator, accelerator);
                }
            };
            subMenu.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F4, KeyEvent.CTRL_MASK));
            JMenuItem item1 = new JMenuItem("Item 1");
            JMenuItem item2 = new JMenuItem("Item 2");
            subMenu.add(item1);
            subMenu.addSeparator();
            subMenu.add(item2);
            topMenu.add(subMenu);
            bar.add(topMenu);
            frame.setJMenuBar(bar);
            frame.setSize(400, 300);
            frame.setVisible(true);
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (InstantiationException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IllegalAccessException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (UnsupportedLookAndFeelException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    new TestMenu().initUI();
                }
            });
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have this Django application in which when user request for localhost:8000/time/ ,it is
I have a ASP.NET website in which a user makes a request. Every request
I have an element(p) which has say 10 children(10 div's).Based on user request i
I have a process where an incoming user request to our system is being
i have an Java J2ME application that does (at user request) create HttpConnections to
I have some code that gets the current logged in user. userID = request.session.get(_auth_user_id)
i have user control, which i render on several views. i want to show
I have a user control which I want to define as a template in
Hello I have a weird issue. I have a user control which I load
I have a django view that looks like... def add_user(request): if User.objects.get(username__exact = request.POST['username']):

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.