Copy shortcut links with ctrl key only in OSX Java application

I created a small application using NetBeans 8.1 on OSX by following these steps:

  • I created a new JForm using the Swing GUI forms category
  • I added three menus:

enter image description here

  • I added JDialog with text fields and linked it to the third menu ("TAnalyse").

In this JDialog, I need copy / paste functions for text fields. The problem is that copy / paste only works in this dialog box with "ctrl" + "c", "x" or "v", and not with the standard "cmd" osx.

I tried adding the following line of code to the JForm constructor, but it did not work:

KeyStroke stroke = KeyStroke.getKeyStroke(KeyEvent.VK_C, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask());

: JDK7 OSX Yosemite. "". ( "", "" ) .

?

Update: GUI NetBeans (Swing GUI Forms → JDialog). JFrame JMenuItem GUI. - :

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);
    }

:

enter image description here

Update2: GUI Netbeans (Swing GUI Forms → Application sample form).

:

enter image description here

, Netbeans ( Java) , .

:

enter image description here

+2
1

Java , . Action DefaultEditorKit action CopyAction. . getMenuShortcutKeyMask(), , .

image

import com.sun.glass.events.KeyEvent;
import java.awt.EventQueue;
import java.awt.Toolkit;
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);
    }
}
+2

All Articles