Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Copy/Paste in JTextField using JMenuItems/Keystrokes

843807Sep 18 2010 — edited Sep 19 2010
I'm having trouble getting this to work, could someone please point me in the right direction? I've made this as much SCCE as possible so it probably won't compile but you can see what I'm trying to do... Neither the menu items nor the keypresses seem to copy or paste.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

public class Main extends JFrame implements ActionListener, KeyListener
{
    private JMenuBar menuBar;
    private JMenu mEdit;
    private JMenuItem copyItem, pasteItem;
    private JTextField output;
    private static final int WIN_WIDTH=300, WIN_HEIGHT=300;

    public Main()
    {
        super("Main");
        this.setSize(WIN_WIDTH,WIN_HEIGHT);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLayout(new BorderLayout());
        this.setJMenuBar(createMenu());
        this.setResizable(false);
        this.setIconImage(new ImageIcon("icon.png").getImage());
        this.addKeyListener(this);
        this.setFocusable(true);

        this.add(createTxtField());
    }

    public Component createTxtField()
    {
        output = new JTextField();
        output.setBounds(5, 5, 240, 30);
        // output.setEditable(false);
        output.setVisible(true);
        output.setEnabled(true);
        output.setText("0.");
        output.setBackground(Color.WHITE);
        output.setHorizontalAlignment(JTextField.RIGHT);
        return output;
    }
    public JMenuBar createMenu()
    {
        // Create the menu bar.
        menuBar = new JMenuBar();
        // Build the first menu.
        mEdit = new JMenu("Edit");
        menuBar.add(mEdit);

        // Build items for first menu.
        copyItem = new JMenuItem("Copy", KeyEvent.VK_C);
        copyItem.setAccelerator(KeyStroke.getKeyStroke(
                KeyEvent.VK_C, ActionEvent.CTRL_MASK));
        copyItem.addActionListener(this);
        copyItem.addKeyListener(this);
        mEdit.add(copyItem);
        //
        pasteItem = new JMenuItem("Paste", KeyEvent.VK_V);
        pasteItem.setAccelerator(KeyStroke.getKeyStroke(
                KeyEvent.VK_V, ActionEvent.CTRL_MASK));
        pasteItem.addActionListener(this);
        pasteItem.addKeyListener(this);
        mEdit.add(pasteItem);

        return menuBar;
    }

    public void actionPerformed(ActionEvent e)
    {
        if (e.getActionCommand().equals("Copy"))
        {
            output.selectAll();
            output.copy();
        }
        else if (e.getActionCommand().equals("Paste"))
        {
            output.selectAll();
            output.paste();
        }
        else
            //  performAction(e.getActionCommand());
    }

    public void keyPressed(KeyEvent e)
    {
        if (e.equals(KeyStroke.getKeyStroke
                (KeyEvent.VK_C, ActionEvent.CTRL_MASK)))
        {
            output.selectAll();
            output.copy();
        }
        else if (e.equals(KeyStroke.getKeyStroke
                (KeyEvent.VK_V, ActionEvent.CTRL_MASK)))
        {
            output.selectAll();
            output.paste();
        }
    }
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 17 2010
Added on Sep 18 2010
8 comments
1,013 views