tags:

views:

693

answers:

2

Hi guys,

Trying to get a tab character into a JMenuItem using \t but it's not printing.

I bet it's something really basic I'm missing. Here's the code

menuItem = new JMenuItem("New\tCtrl + N");

Thanks

+5  A: 

You're not supposed to add keyboard shortcuts manually, there's an API for it, that puts them in the proper place. Look here, for instance, or search the Swing docs for "accelerators".

unwind
+14  A: 

Try this instead:

menuItem = new JMenuItem("New");
menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.CTRL_MASK));

You should also add mnemonics for usability:

menuItem.setMnemonic(KeyEvent.VK_N);

See the Java Look and Feel Guidelines for greater clarification, especially volume 1.

David Grant
Alternative: `KeyStroke.getKeyStroke("control N")`
finnw