Image

Imageska_o wrote in Imagejava_dev

Accelerator keys (Swing)

Hi all,

I'm trying to add shortcuts to popup menu items. The popup menu appears in an internal frame. The code is like this:

JPopupMenu result = ....
....
JMenuItem copyItem = new JMenuItem("Copy...");
copyItem.setAccelerator(KeyStroke.getKeyStroke("control C"));
copyItem.setActionCommand(COPY_COMMAND);
copyItem.addActionListener(this);
result.add(copyItem);

For some reason, the shortcut key works only when the menu is activated, which kind of defeats its purpose. In this case, I have to right-click on the frame to make the menu show up, and only then will Ctrl-C work.

Similar code works properly for a pull-down menu in a desktop pane (main application frame), i.e. I don't have to pull down the menu to open a file with Ctrl-O.

I can make the shortcut keys work in the popup menu above by using the following code (chartPanel is contained in an internal frame.):


chartPanel.getInputMap().put(KeyStroke.getKeyStroke("control C"), "copy");
chartPanel.getActionMap().put("copy", new ActionDelegate(ChartPanelWithCopy.COPY_COMMAND));


The advantage of using the setAccelerator method is that the shortcut shows up on the menu nicely arranged, "Copy... Ctrl-C". Any ideas how to debug or fix this (besides using both the input map and the setAccelerator method)?

For those that are familiar with the JFreeChart library, I'm modifying the ChartPanel class.

Thanks in advance!