Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
972 views
in Technique[技术] by (71.8m points)

macos - copy paste shortcuts only working with ctrl key in OSX Java application

I created a small application using Netbeans 8.1 on OSX, doing these steps:

  • I created a new JForm using category "Swing GUI forms"
  • I added three menus to it:

enter image description here

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

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:

enter image description here

Update2: I created another small example with Netbeans GUI builder (Swing GUI Forms -> Application sample form).

The result is:

enter image description here

Finally i created an example with Netbeans (Empty Java file) with source code slightly modified from the answer below.

The result is:

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

image

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...