Ahmad Hussnain Ahmad Hussnain - 1 year ago 107
Java Question

Java: Right Click Copy Cut Paste On TextField

I've made a program which prints a value in a text field. The problem is that when a user right clicks in the text field, a menu like this won't open:

enter image description here

Is there a way the user can have this menu open upon right click?

This is my code:

public class A extends JFrame{
private JTextField txt1;
private JTextField txt2;
private JLabel val;
private JLabel prt;
private JButton bt1;

public A() {


txt1 = new JTextField();
txt1.setBounds(178, 93, 87, 28);

txt2 = new JTextField();
txt2.setBounds(178, 148, 87, 28);

val = new JLabel("Enter Value");
val.setBounds(84, 93, 69, 28);

prt = new JLabel("Printed Value");
prt.setBounds(80, 148, 87, 28);

bt1 = new JButton("Click This");
bt1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {

int n=Integer.parseInt(txt1.getText());

bt1.setBounds(178, 188, 105, 28);

setSize(400, 399);


Main Method:

public class Main {

public static void main(String[] args) {

A object = new A();


Answer Source

Read the Swing tutorial on How to Use Menus for the basics of creating a popup menu.

Then you can use the Actions provided by the DefaultEditorKit to create your popup menu.

For the "Delete" action you will need to create your own custom Action. Read the Swing tutorial on How to Use Actions for the basics. Except you would extend TextAction since it has methods that allow you to access the text component with focus so you can create reusable code.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;

public class TextFieldPopup extends JPanel
    public TextFieldPopup()
        JTextField textField = new JTextField(10);
        add( textField );

        JPopupMenu menu = new JPopupMenu();
        Action cut = new DefaultEditorKit.CutAction();
        cut.putValue(Action.NAME, "Cut");
        cut.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke("control X"));
        menu.add( cut );

        Action copy = new DefaultEditorKit.CopyAction();
        copy.putValue(Action.NAME, "Copy");
        copy.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke("control C"));
        menu.add( copy );

        Action paste = new DefaultEditorKit.PasteAction();
        paste.putValue(Action.NAME, "Paste");
        paste.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke("control V"));
        menu.add( paste );

        Action selectAll = new SelectAll();
        menu.add( selectAll );

        textField.setComponentPopupMenu( menu );

    static class SelectAll extends TextAction
        public SelectAll()
            super("Select All");
            putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke("control S"));

        public void actionPerformed(ActionEvent e)
            JTextComponent component = getFocusedComponent();

    private static void createAndShowGUI()
        JFrame frame = new JFrame("TextFieldPopup");
        frame.add( new TextFieldPopup() );
        frame.setLocationByPlatform( true );
        frame.setVisible( true );

    public static void main(String[] args)
        EventQueue.invokeLater(new Runnable()
            public void run()
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download