0

I am coding Java Swing Calculator in NetBeans. I have JFrame, calculator buttons and JTextField called display. I need to support the copy (and also CTRL+C) option. Does anyone have an idea on how to do this?

Stack Player
  • 1,470
  • 2
  • 18
  • 32
  • 3
    Do you mean copy to your clipboard? That's been answered many times before, for example [here](http://stackoverflow.com/questions/6710350/copying-text-to-the-clipboard-using-java) – Lukas Rotter Aug 22 '15 at 21:19

1 Answers1

4

If you want to add a right-click menu for cut/copy/paste, you can use the Cut/Copy/Paste actions that your components already have, although I prefer to rename them to give them simpler easier to read names, since it's easier to read "Cut" rather than "cut-to-clipboard".

For instance, if you call this method and pass in any text component, it should add a right-click pop-up menu for cut-copy-paste:

// allows default cut copy paste popup menu actions
private void addCutCopyPastePopUp(JTextComponent textComponent) {
   ActionMap am = textComponent.getActionMap();
   Action paste = am.get("paste-from-clipboard");
   Action copy = am.get("copy-to-clipboard");
   Action cut = am.get("cut-to-clipboard");

   cut.putValue(Action.NAME, "Cut");
   copy.putValue(Action.NAME, "Copy");
   paste.putValue(Action.NAME, "Paste");

   JPopupMenu popup = new JPopupMenu("My Popup");
   textComponent.setComponentPopupMenu(popup);
   popup.add(new JMenuItem(cut));
   popup.add(new JMenuItem(copy));
   popup.add(new JMenuItem(paste));
}

For example:

import java.awt.BorderLayout;
import javax.swing.*;
import javax.swing.text.JTextComponent;

public class AddCopyAndPaste extends JPanel {
    private JTextField textField = new JTextField("Four score and seven years ago...");
    private JTextArea textArea = new JTextArea(15, 30);

    public AddCopyAndPaste() {
        addCutCopyPastePopUp(textField);
        addCutCopyPastePopUp(textArea);

        setLayout(new BorderLayout());
        add(textField, BorderLayout.PAGE_START);
        add(new JScrollPane(textArea), BorderLayout.CENTER);
    }

    // allows default cut copy paste popup menu actions
    private void addCutCopyPastePopUp(JTextComponent textComponent) {
       ActionMap am = textComponent.getActionMap();
       Action paste = am.get("paste-from-clipboard");
       Action copy = am.get("copy-to-clipboard");
       Action cut = am.get("cut-to-clipboard");

       cut.putValue(Action.NAME, "Cut");
       copy.putValue(Action.NAME, "Copy");
       paste.putValue(Action.NAME, "Paste");

       JPopupMenu popup = new JPopupMenu("My Popup");
       textComponent.setComponentPopupMenu(popup);
       popup.add(new JMenuItem(cut));
       popup.add(new JMenuItem(copy));
       popup.add(new JMenuItem(paste));
    }

    private static void createAndShowGui() {
        AddCopyAndPaste mainPanel = new AddCopyAndPaste();

        JFrame frame = new JFrame("Add Copy And Paste");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGui();
            }
        });
    }
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373