invisible JMenuBar, Accelerator not working

luckydonald

My Program has a JMenuBar with JMenuItems. They have a ActionListener, and I set a Shortcut with setAccelerator. Now I am hiding the menu bar when the window become unfocused, to get more space for a displayed image. But after the first hiding of the menubar, the hotkeys just stop working.

How can I fix that?

I created a little example code to illustrate that strange behavior:

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

class Example extends JFrame{
    public static void main(String[] args) {
        new Example(); //main is static
    }
    static JMenuBar menubar; //be accessable for the ActionListener
    Example() {
        //JPanel
        this.setSize(50,50);
        this.setVisible(true);  

        //Menubar, static
        menubar = new JMenuBar();
        this.setJMenuBar(menubar);

        //Menu
        JMenu filemenu = new JMenu("File");
        menubar.add(filemenu);

        //Item
        JMenuItem menuitem = new JMenuItem("Do Something...");
        filemenu.add(menuitem);
        menuitem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_D, KeyEvent.SHIFT_DOWN_MASK)); // Shift + D
        menuitem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("Action!");
            }
        });

        JButton button = new JButton("Show/Hide menubar");
        this.add(button);
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("Toggle Bar!");
                menubar.setVisible(!menubar.isVisible()); //Toggle
            }
        });
    }
}

For reference: I'm using Java 1.7.0_60-ea (Java 7) on a Mac. But this error occurs independent of using the Mac native menu bar or the normal java menu bar inside the JFrame.

Brainstorm

You could try to add global keybindings. How to add keybindings is explained here.

Here is an example of what you could do:

//Any component that is always visible in the window (like the image)
JComponent c;
//Get input and action map
InputMap imap = c.getInputMap(WHEN_IN_FOCUSED_WINDOW);
ActionMap amap = c.getActionMap();
//Put keybinding and action
imap.put(KeyStroke.getKeyStroke("shift D"), "doSomething");
amap.put("doSomething", anAction);

Note that it only works in the focused window. But should work regardless of the menubar being visible or not.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

invisible JMenuBar, Accelerator not working

From Dev

MFC: Why is the Accelerator not working?

From Dev

OpenERP attrs invisible is not working

From Dev

OpenERP attrs invisible is not working

From Dev

c++ Keyboard accelerator function not working

From Dev

Accelerator commands not working when focus lost

From Dev

Screen capturing is not working when windows form is invisible

From Dev

Always visible textview , invisible not working in android

From Dev

VNC Address Book - Window Invisible? Working as intended?

From Dev

How to get the menu item accelerator keys working when using the screen menu bar on OS X

From Dev

jquery.steps and validation not working - form content not visible (invisible)

From Dev

Three.js invisible plane not working with raycaster.intersectObject

From Dev

Java JMenuBar repainting background

From Dev

Get JMenuItems from JMenuBar

From Dev

Changing a JMenuBar's font

From Dev

JMenuBar is / is not Displayed in sucsessive runs

From Dev

JMenuBar not showing when it should

From Dev

JMenuBar at the top of BorderLayout

From Dev

java JMenuBar not visible? Why?

From Dev

Java JMenuBar repainting background

From Dev

Changing a JMenuBar's font

From Dev

JMenuBar not showing when it should

From Dev

JLabel overwrites JMenuBar

From Dev

Error with JMenuBar (duplicates and overlaps)

From Dev

Android setVisibility View.INVISIBLE and View.GONE aren't working

From Dev

Background color of parent tr of table not working if its child cells(td) are invisible, for chrome browser

From Dev

Background color of parent tr of table not working if its child cells(td) are invisible, for chrome browser

From Dev

PySide: Assign accelerator to `QTextEdit`

From Dev

Fully utilizing HW accelerator

Related Related

  1. 1

    invisible JMenuBar, Accelerator not working

  2. 2

    MFC: Why is the Accelerator not working?

  3. 3

    OpenERP attrs invisible is not working

  4. 4

    OpenERP attrs invisible is not working

  5. 5

    c++ Keyboard accelerator function not working

  6. 6

    Accelerator commands not working when focus lost

  7. 7

    Screen capturing is not working when windows form is invisible

  8. 8

    Always visible textview , invisible not working in android

  9. 9

    VNC Address Book - Window Invisible? Working as intended?

  10. 10

    How to get the menu item accelerator keys working when using the screen menu bar on OS X

  11. 11

    jquery.steps and validation not working - form content not visible (invisible)

  12. 12

    Three.js invisible plane not working with raycaster.intersectObject

  13. 13

    Java JMenuBar repainting background

  14. 14

    Get JMenuItems from JMenuBar

  15. 15

    Changing a JMenuBar's font

  16. 16

    JMenuBar is / is not Displayed in sucsessive runs

  17. 17

    JMenuBar not showing when it should

  18. 18

    JMenuBar at the top of BorderLayout

  19. 19

    java JMenuBar not visible? Why?

  20. 20

    Java JMenuBar repainting background

  21. 21

    Changing a JMenuBar's font

  22. 22

    JMenuBar not showing when it should

  23. 23

    JLabel overwrites JMenuBar

  24. 24

    Error with JMenuBar (duplicates and overlaps)

  25. 25

    Android setVisibility View.INVISIBLE and View.GONE aren't working

  26. 26

    Background color of parent tr of table not working if its child cells(td) are invisible, for chrome browser

  27. 27

    Background color of parent tr of table not working if its child cells(td) are invisible, for chrome browser

  28. 28

    PySide: Assign accelerator to `QTextEdit`

  29. 29

    Fully utilizing HW accelerator

HotTag

Archive