Alex Kibler
Alex Kibler

Reputation: 4954

How do I set keyboard shortcuts for my menubar in a Java application?

I'm developing a simple drawing application in Java. I'm restricted to using an AWT MenuBar (Not JMenuBar), and I need to set a few keyboard shortcuts and I can't find how to do so. I need shortcuts for two MenuItems and for three CheckBoxMenuItems.

It would go something like this:

newItem = Ctrl+N
quitItem = Ctrl+Q
rectangleItem = Ctrl+R
ovalItem = Ctrl+O
lineItem = Ctrl+L

Upvotes: 1

Views: 764

Answers (2)

Jeff
Jeff

Reputation: 12795

Since the suggestion in the comments seems to have fixed at least part of your problem, I'm gonna put it here as an answer for users in the future.

Read the second half of this documentation and change your MenuItem constructors to take in a MenuShortcut as their second argument, like this (taken from the linked docs):

menu.add(new MenuItem("Save...", new MenuShortcut('s'));

Upvotes: 1

user529543
user529543

Reputation:

it would be easyer if:

put a layered pane? a glass in front, which will accept keyboard event in the whole area.

  • not sure what components is for awt, but for sure you have a very good reason to use it, not just look and feel?

There you set the shortcuts.

Upvotes: 0

Related Questions