4

I have added a JPopupMenu with certain JMenuItems to a JTable in my java application. I then set accelerators for those JMenuitems. Now my problem is that when I try to use those hotkeys, they don't work until I open that JPopupMenu. When that popup menu is open at that time, the hotkey works properly which is really useless. Please help me.

troy_frommer
  • 102
  • 1
  • 21
user2067787
  • 130
  • 8

1 Answers1

3

The accelerator bindings only work on components added to the GUI. A popup menu is not added to the GUI until the popup has been invoked.

You can try:

  1. Add you popup menu to the menuBar of your frame. Then the accelerator will be recognized
  2. Manually add the KeyBindings for each accelerator in you popup menu. Read the Swing tutorial on How to Use Key Bindings for more information.
camickr
  • 321,443
  • 19
  • 166
  • 288
  • 1st solution is not possible because my app has only one jframe rest are the jpanels that are dynamically loaded. I tried the second option also and it didn't work. – user2067787 Jul 19 '13 at 14:28
  • +1 @user2067787: For reference, [`ImageApp`](http://stackoverflow.com/a/5129757/230513) is an example that uses the same `Action` for both menu and popup. – trashgod Jul 19 '13 at 15:43
  • `jpanels that are dynamically loaded` - you can dynamically update the menubar when you load the panel. `I tried the second option also and it didn't work` - post your SSCCE demonstrating the problem when you use Key Bindings. – camickr Jul 19 '13 at 17:15