开发者

Java: How to create a paste MenuItem with accelerator?

When I create a JMenuBar with a Paste action in it and assign an accelerator to it (Control - V) and I write the paste action for the JMenuItem. AND I use the accelerator, my code will be executed and the Swing preprogrammed Paste action will开发者_StackOverflow中文版 be executed, so the text will be pasted twice, I think.

I didn't test it but, it looks very logical that this would be the problem.

Thanks


Swing works by using Key Bindings. So only one action is invoked depending on which key binding is found. So you wound not get the text pasted twice.

You may have a different Action invoked depending on which component has focus and on how the Action was invoked.

For example, if focus is on the text component, then I would guess that the default Paste action will be used.

If you click on the menu item, then the action you added to the menu item would be invoked.

If you invoke Control+V when focus in not on a text field, then I would guess the Action bound to the menu item would be invoked because accelerators use the "in focus window" (or whatever its called) InputMap.

If you are using the default Actions provided by the EditorKit you will not have a problem:

JMenuItem pasteAction = new JMenuItem( new DefaultEditorKit.PasteAction() );
pastAction.setAccelerator(...);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜