Re: Printable character as JMenuItem accelerator not properly consumed
Martin wrote:
I would still expect the swing API to support such an
implementation. If - what I find a bit strange - the key stroke which
triggered a menu item as an accelerator is not consumed before
entering the action associated with the menu item,
Did you try my earlier suggestion:
you might want to consider replacing
item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, 0));
with
item.setAccelerator(KeyStroke.getKeyStroke('n'));
... It ... prevents 'n' being interpreted as BOTH a literal
character and an accelerator.
Compile this, run it and press n. Now press n again.
--------------------------------- 8< --------------------------
public class TestAccellerator2 {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new TestAccellerator2();
}
});
}
TestAccellerator2() {
final JCheckBox box = new JCheckBox("Pencils");
final JTextArea area = new JTextArea(5, 40);
JMenuBar bar = new JMenuBar();
JMenu menu = new JMenu("Do");
JMenuItem item = new JMenuItem("Nothing");
item.setMnemonic(KeyEvent.VK_N);
item.setAccelerator(KeyStroke.getKeyStroke('n'));
item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println(e.getActionCommand() + " pressed.");
area.requestFocus();
}
});
menu.add(item);
bar.add(menu);
JPanel p = new JPanel();
p.add(box);
p.add(area);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
box.requestFocus();
}});
JFrame f = new JFrame("Test Menu Accelerator");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setJMenuBar(bar);
f.add(p);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}
--------------------------------- 8< --------------------------
I am wondering why you are not producing SSCCEs of your own.