I created a small application using Netbeans 8.1 on OSX, doing these steps:
In this JDialog i need copy / paste functionality for the text fields. The problem is: copy / paste only works in this dialog with "ctrl" + "c","x" or "v" and not with the osx standard "cmd" key.
I tried to add the following line of code to the constructor of the JForm but it didn't work:
KeyStroke stroke = KeyStroke.getKeyStroke(KeyEvent.VK_C, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask());
Additional information: I am using JDK7 and OSX Yosemite. Look and feel is "Nimbus". The two other menus ("File","Edit") aren't implemented yet.
Can you give a hint for a solution?
Update: I created another small example with Netbeans GUI builder (Swing GUI Forms -> JDialog). I just added a menu bar to the JFrame and the a JMenuItem in the GUI builder. With the remarks from the answer below i added manually some code to the constructor:
public NewJDialogGUI(java.awt.Frame parent, boolean modal) {
super(parent, modal);
initComponents();
AbstractAction copyAction = new DefaultEditorKit.CopyAction();
copyAction.putValue(Action.ACCELERATOR_KEY,KeyStroke.getKeyStroke(KeyEvent.VK_C, MASK));
this.jMenuItem1.setAction(copyAction);
this.jMenuItem1.setText("Copy");
this.jMenuItem1.setMnemonic(KeyEvent.VK_C);
}
The result is:
Update2: I created another small example with Netbeans GUI builder (Swing GUI Forms -> Application sample form).
The result is:
Finally i created an example with Netbeans (Empty Java file) with source code slightly modified from the answer below.
The result is:
Java uses Actions to encapsulate functionality and Key Bindings to respond to keys typed by the user. In this example, the DefaultEditorKit
action CopyAction
is used as the menu item's Action
. It will copy the user's selection from the focused text component to the clipboard. Use Toolkit.getMenuShortcutKeyMask()
to get the correct accelerator, as discussed here.
import java.awt.EventQueue;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.text.DefaultEditorKit;
/**
* @see https://stackoverflow.com/a/34830519/230513
*/
public class MenuTest {
private static final int MASK
= Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
private void display() {
JFrame f = new JFrame("Test");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("Edit");
menu.setMnemonic(KeyEvent.VK_E);
JMenuItem menuItem = new JMenuItem();
AbstractAction copyAction = new DefaultEditorKit.CopyAction();
copyAction.putValue(Action.ACCELERATOR_KEY,
KeyStroke.getKeyStroke(KeyEvent.VK_C, MASK));
menuItem.setAction(copyAction);
menuItem.setText("Copy");
menu.add(menuItem);
menuBar.add(menu);
f.setJMenuBar(menuBar);
f.add(new JTextField(10));
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new MenuTest()::display);
}
}