1

JTextArea by default has Copy/Paste functionality, but you need to use shortcut keys (e.g Cntl-C/V on Windows) to access. I need to add popupup to access functionality for more niave users.

Done this and it works on Windows, but is this portable, are the actions copy-to-clipboard/paste-from-clipboard available on all platforms ?

public class CopyPastePopup extends JPopupMenu
{
    public CopyPastePopup(JTextComponent ta)
    {
        Action copy  = ta.getActionMap().get("copy-to-clipboard");
        copy.putValue(NAME, TextLabel.COPYBUTTON.getMsg());
        copy.putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_C, Toolkit.getDefaultToolkit().getMenuShortcutKeyMaskEx()));

        Action paste = ta.getActionMap().get("paste-from-clipboard");
        paste.putValue(NAME, TextLabel.PASTEBUTTON.getMsg());
        paste.putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_V, Toolkit.getDefaultToolkit().getMenuShortcutKeyMaskEx()));

        add(copy);
        add(paste);
    }
}

Use as

mask.setComponentPopupMenu(new CopyPastePopup(mask));
Paul Taylor
  • 13,411
  • 42
  • 184
  • 351
  • No idea about your actual question, sorry. But still, are you aware that this exists? https://stackoverflow.com/questions/6710350/copying-text-to-the-clipboard-using-java – Petr Janeček Nov 03 '21 at 11:42
  • 1
    yeah but that seems to be reimplementing copy paste , it seemed cleaner if i could just link the popupmenu to the existing copy/paste implnentation for JTextfield – Paul Taylor Nov 03 '21 at 12:05
  • Aha, only now I can properly see what you're doing. Nice. Um, looks like you should be fine according to the docs: https://docs.oracle.com/en/java/javase/17/docs/api/constant-values.html#javax.swing.text.DefaultEditorKit.copyAction, found via https://docs.oracle.com/en/java/javase/17/docs/api/java.desktop/javax/swing/text/DefaultEditorKit.html#copyAction and https://docs.oracle.com/en/java/javase/17/docs/api/java.desktop/javax/swing/text/DefaultEditorKit.CopyAction.html. Good luck! – Petr Janeček Nov 03 '21 at 13:09

0 Answers0