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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:56:12+00:00 2026-06-14T14:56:12+00:00

I try to make my JMenuBar to activate first JMenu on Alt KeyEvent ,

  • 0

I try to make my JMenuBar to activate first JMenu on Alt KeyEvent, but without opening popup, so that one could open popup with arrows keystrokes later. Just like it is done in NetBeans, Mozilla, any other program window.

Here is the code that works not as intended. The worst side effect is that it reacts on alt+tab combination, and it definitely should not popup menu on alt+tab. I just need to make a menu go to the “armed” state and be able to traverse menus by arrow keys (arrows right & left to “arm” menus and arrow down to open “armed” menu popup). Is there any simple way to make this happen?

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;

public class Test5 extends JFrame {

    public Test5() {
        super("test");
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        JPanel pan = new JPanel(new BorderLayout());

        final JMenuBar bar = new JMenuBar();
        final JMenu menu = new JMenu("File", false);

        menu.setMnemonic(KeyEvent.VK_ALT);

        JMenuItem item = new JMenuItem("All");
        JMenuItem item2 = new JMenuItem("Exit");
        menu.add(item);
        menu.add(item2);

        JMenu menu1 = new JMenu("Test");
        JMenuItem item1 = new JMenuItem("All");
        menu1.add(item1);

        bar.add(menu);
        bar.add(menu1);

        setJMenuBar(bar);

        setSize(200, 200);
        setVisible(true);
    }


    public static void main(String[] args) {
        new Test5();
    }

}

Solved thanks to Guillaume Polet:
There is some code in com.sun.java.swing.plaf.windows.WindowsLookAndFeel class, wich works with Alt keystrokes:

public void initialize() {
    super.initialize();
    // some more initialization here
    KeyboardFocusManager.getCurrentKeyboardFocusManager()
                        .addKeyEventPostProcessor(WindowsRootPaneUI.altProcessor);
}

And the AltProcessor class does all the magic.

If you don’t have any custom LaF, you can just use WindowsLookAndFeel as it is, or there is proper example how to process Alt events in menus for your own special LaF.

  • 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-14T14:56:13+00:00Added an answer on June 14, 2026 at 2:56 pm

    Before starting your GUI, invoke this line:

    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    

    And remove the mnemonic.

    This will automatically install the desired behaviour on Windows.
    If you need this on all platforms, then you will have to go with KeyBindings, but since this behaviour is only observed on Windows, I don’t find it problematic to recreate it only on Windows.

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

Sidebar

Related Questions

I try to make a useful macro for logging. But I find that NSLog
I try to make an imageview clickable. Actually it is clickable, so that I
I try to make a apps that need barcode scanner, i already can get
I try to make an animation with an image in my view but it
I try to make this implementation of a multimap in C++, but I run
I try to make a slider but have trouble, with placing all my div
I try to make some databinding involving an x:Array defined in the resources, but
I try to make following: Horizontal list of images, that I can select and
I try to make a simple test with spring/jpa/hibernate and GAE/Google Cloud SQL. But
I try to make redirect in my folder: Redirect /promotion/ /newpromotion Redirect /promotion/first/ /new/first/

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.