• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JTable & Keyboard Shortcuts

Bj�rn Kaidel
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I'm writing a little program with a GUI. It's the first time that im using a JTable and so far everything worked as I wanted it to. But there's one problem I can't seem to solve:
Whenever the JTable is focused, no keyboard shortcuts (for my menu, i.e. "Save" etc.) will be processed. The reason is propably that JTable has a very good keyboard support with many built-in functions (which is pretty cool) and therefore doesn't process the keyboard input. But I still want to be able to use my own keyboard shortcuts for my menus etc. Mnemonics work fine, but accelerators don't work at all.

How can I tell my JTable that it should ignore every keyboard input if the control, alt or a combination of alt, control and shift is pressed?

Someone on another forum told me that I should use the Input- & ActionMap classes, but that doesn't really work. So far im only able to process my keyboard input if ONLY the control key is pressed by manually shifting the keyboard focus from the table to the menubar and shifting it back onto the table after the control key is released. That doesn't work if im pressing shift before control, though.
I also have to use: KeyStroke.getKeyStroke(KeyEvent.VK_CONTROL,2,false)
to get the InputMap to recognize the control key input. As you can see I have to put control as the KeyEvent AND the modifiter. That's the only way that it will work. If I try to add another modifier (i.e. shift, alt), the InputMap will not recognize it. The same goes for shift. It only works if I specifiy shift as the modifier and the KeyEvent. I haven't tried alt yet, but it will propably be the same.

It's a start that I got the control key to work, because if the user first presses the control key and THEN all other shortcut key it works without a problem (for example control, then shift and 'a' for adding a row to the table). But if he presses shift first... well, nothing will happen
And my "focus-shifting" is kinda clumsy, because the user sees that the JTable loses the focus and regains focus after control is released (and that only works if control is released as the last button. If control is released before the user releases all other keys the InputMap of my menubar [that I configured by using a boolean variable to process the focus back onto the table after control is released if the focus came from the table] will not recognize the input).
So the user always has to press control as the first button and release it last. And thats not really good.

The only solution that I see at the moment is to define ALL my keyboard shortcuts again in the Action- and InputMap of the JTable. But thats a lot of work and it would be really annoying to implement all actions and shortcuts twice (for the menu AND the table).

I would really appreciate it if anyone of you could help me. I'm stuck with this problem for almost a week now and I really can't find any way to solve it effectivly.

Many thanks in advance for your help!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic