How to get focus in JPopupMenu

I am an experienced Java programmer but new to Swing, so please bear with me.

I want to have a JPopupWindow that has keyboard focus. I want to answer the arrow keys, run (to close the menu) and Enter (to call the item). Should I add a KeyListener to the menu and encode it all myself, or is there some kind of β€œmode” that I can configure to activate this behavior, which seems to be standard.

In the standard section of the Swing manual, only mnemonics and accelerators speak JPopupMenus. I really don't want this.

It is hard to believe that it is completely against JPopupMenu grain.

What is the best way to get something like I want to implement?

+6
source share
2 answers

As soon as you make jpopupMenu.show(Component invoker, int x, int y) , it will automatically answer the keyboard the way you want. If this does not work, you may need to fix some other problems. Ie another component grabs the focus from the menu when it shouldn't, etc.

+6
source

Instead of KeyListener I would recommend adding Action to your JPopupMenu . Use setComponentPopupMenu() to add a popup to the panel.

+3
source

Source: https://habr.com/ru/post/903778/


All Articles