Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Right-click to copy and paste in Java

Tags:

java

swing

I am using Netbeans to develop an application that will be used in Windows. I noticed I can't right-click to copy or paste. How can I enable this? (I am using basic Swing controls such as JText and JTextArea.)

like image 381
averageman Avatar asked Jan 26 '12 16:01

averageman


People also ask

How do I copy and paste in Java?

Select "File -> Exit" from the Policy Tool window to close the policy editor. Restart your browser. You should now be able to copy and paste between Java Swing applets and other computer applications such as Microsoft Excel. Please note to use Ctrl-C and Ctrl-V to copy and paste.

How do I copy a JFrame?

Open the design tab of the frame you want to copy, then hold Shift and drag the mouse over all the design elements. You can then copy them all and paste in another JFrame.


4 Answers

Why right click is not working on java application?

I wouldn't create new copy, cut, paste, undo & select all actions, because those already exist inside the ActionMap of each component. I'd simply do:

 Action copyAction = textField.getActionMap().get("copy");
 Action cutAction = textField.getActionMap().get("cut");
 Action pasteAction = textField.getActionMap().get("paste");
 Action undoAction = textField.getActionMap().get("undo");
 Action selectAllAction = textField.getActionMap().get("selectAll");


 popup.add (undoAction);
 popup.addSeparator();
 popup.add (cutAction);
 popup.add (copyAction);
 popup.add (pasteAction);
 popup.addSeparator();
 popup.add (selectAllAction);

 return popup;

That way you don't recreate more code that's already written. Other than that I'd follow that example.

like image 146
chubbsondubs Avatar answered Sep 29 '22 16:09

chubbsondubs


Static class to instantly add regular popup menu to a textfield.

Just call JTextFieldRegularPopupMenu.addTo(jTextFieldObj);

import javax.swing.*;
import java.awt.event.ActionEvent;
import javax.swing.JPopupMenu;
import javax.swing.undo.*;


public class JTextFieldRegularPopupMenu {
    public static void addTo(JTextField txtField) 
    {
        JPopupMenu popup = new JPopupMenu();
        UndoManager undoManager = new UndoManager();
        txtField.getDocument().addUndoableEditListener(undoManager);

        Action undoAction = new AbstractAction("Undo") {
            @Override
            public void actionPerformed(ActionEvent ae) {
                if (undoManager.canUndo()) {
                    undoManager.undo();
                }
                else {
                    JOptionPane.showMessageDialog(null,
                            "Undoable: " + undoManager.canUndo() ,
                            "Undo Status", 
                            JOptionPane.INFORMATION_MESSAGE);
                }
            }
        };

       Action copyAction = new AbstractAction("Copy") {
            @Override
            public void actionPerformed(ActionEvent ae) {
                txtField.copy();
            }
        };

        Action cutAction = new AbstractAction("Cut") {
            @Override
            public void actionPerformed(ActionEvent ae) {
                txtField.cut();
            }
        };

        Action pasteAction = new AbstractAction("Paste") {
            @Override
            public void actionPerformed(ActionEvent ae) {
                txtField.paste();
            }
        };

        Action selectAllAction = new AbstractAction("Select All") {
            @Override
            public void actionPerformed(ActionEvent ae) {
                txtField.selectAll();
            }
        };

        popup.add (undoAction);
        popup.addSeparator();
        popup.add (cutAction);
        popup.add (copyAction);
        popup.add (pasteAction);
        popup.addSeparator();
        popup.add (selectAllAction);

       txtField.setComponentPopupMenu(popup);
    }
}
like image 40
Karlo Kokkak Avatar answered Sep 29 '22 17:09

Karlo Kokkak


For your comment,

Does it interact with the operating system? I want to copy something from my Java application and paste on Notepad..

Why bother the user to manuallly copy-paste, why not write the contents of textarea to a text file?

like image 25
COD3BOY Avatar answered Sep 29 '22 16:09

COD3BOY


final int colx;
final int rowy;
final String val1;

colx = CFDTable.getSelectedColumn();
rowy = CFDTable.getSelectedRow();

val1 = (String) CFDTable.getValueAt(rowy, colx);
JPopupMenu jPopupMenu = new javax.swing.JPopupMenu();
jPopupMenu.setName("jPopupMenu");
CFDTable.setComponentPopupMenu(jPopupMenu);
JMenuItem jMenuItem = new javax.swing.JMenuItem();
jMenuItem.setText("Copy"); // NOI18N
jMenuItem.setName("jMenuItem"); // NOI18N
jPopupMenu.add(jMenuItem);

jMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
StringSelection entry = new StringSelection(val1);
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(entry, entry);
}
});
like image 22
shruti Avatar answered Sep 29 '22 16:09

shruti