• Post Reply Bookmark Topic Watch Topic
  • New Topic

Mnemonic shortcuts not consumed

 
Helge Petersen
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After updating my Swing application to Java 7 (better late than never), it seems that mnemonic shortcuts are not being consumed.
As a result of this, the menu (JMenuBar) gets focus after using an Alt+key shortcut. Pushing Alt-key again puts focus in targeted field.

Why is the Alt-keyevent not consumed? And how do I fix this?
 
Rob Camick
Ranch Hand
Posts: 2700
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I never noticed this when using JDK7 and don't notice it now using JDK8.

I use Alt+F to select the file menu and then select an menu item. When the menu closes focus is back on the original component.

Post your SSCCE that demonstrates the problem.
 
Campbell Ritchie
Marshal
Posts: 52590
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
Helge Petersen
Greenhorn
Posts: 3
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have attached an example that seems to contain the problem.

Steps
1: Alt+1 puts cursor and focus in Field 1.
2: Alt+2 puts cursor in Field 3. When Alt-key is released it puts focus on File menu.
3: Pushing Alt-key again puts focus back in Field 3.
4: Problem will repeat it self.

Hope it helps to demonstrate the problem.

Problem is tested and found using both JRE7 and JRE8 in Windows XP and Windows 7.

And thanks for the warm welcome



 
Campbell Ritchie
Marshal
Posts: 52590
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I added code tags to your post and broke the very long lines and doesn't it look better now

Find a copy of Core Java2 (vol 1) by Cai Horstmann and Gary Cornell and find the GBC class (also available from Horstmann's website) which reduces the need for those many new constraints objects.
I ran your code, having changed the empty catch (‍) to read e.printStackTrace(); on Ubuntu (14.04LTSx64) JDK1.8.0_45 and bash and was unable to reproduce your problem.
Alt‑1→focus on field 1. (Similarly for 3).
Alt‑2→no effect.
Alt‑F→file menu opens with focus on exit item.
Pressing and releasing alt key→no effect.

May be a specific Windows® phenomenon.
 
Campbell Ritchie
Marshal
Posts: 52590
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Managed to reproduce your problem on Windows7x64 on JDK8u45; releasing the Alt key causes focus to move to the File menu an pushing enter causes exit to appear.
 
Rob Camick
Ranch Hand
Posts: 2700
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Windows LAF appears to bind the "Alt" key to an Action to select the first menu on the menu bar.

To disable this behaviour you can use Key Bindings:



Read the section from the Swing tutorial on How to Use Key Bindings to understand how this works.
 
Helge Petersen
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't want to disable this behavior totally, only to ignore it when when a mnemonic shortcut (Alt+key) is used.
Pushing only Alt should still put focus in menubar.

Using Key Bindings semems to be all or nothing!?
 
Rob Camick
Ranch Hand
Posts: 2700
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is two different KeyStrokes.

1. Alt is a KeyStroke that activates the menu
2. Then when you press "3" you have a combined Alt+3.

So you have two separate Actions. How does the program know you are going to press 1 or 3 after pressing Alt? So how can it ignore Alt sometimes but not other times?

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!