File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes JTextFiled and Jpopupmenu Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTextFiled and Jpopupmenu" Watch "JTextFiled and Jpopupmenu" New topic

JTextFiled and Jpopupmenu

tausahmed ahmed

Joined: Dec 19, 2002
Posts: 15
I want that on right click of my mouse on the JTextField one popup menu get opened which is having the option Copy,Paste,Select All. And I will also be able to perform the copy paste & select all operations.
Vikalp Setya

Joined: May 10, 2003
Posts: 18
I have written a small class that solved your problem. see the code below :
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.text.DefaultEditorKit;
public class MessagePoster extends JFrame
private JPopupMenu menu = new JPopupMenu();
private JTextField textField = new JTextField(30);
public MessagePoster()
Action[] actions = textField.getActions();
for(int i=0;i<actions.length;i++)
String name = actions[i].getValue(Action.NAME).toString();
if(name.equals(DefaultEditorKit.cutAction) ||
name.equals(DefaultEditorKit.copyAction) ||
name.equals(DefaultEditorKit.pasteAction) )
textField.addMouseListener(new MouseAdapter()
public void mouseReleased(MouseEvent me)
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(2,1));
panel.add(new JLabel("Enter some text:"));
public static void main(String[] args)
final JFrame frame = new MessagePoster();
frame.addWindowListener(new WindowAdapter()
public void windowClosing(WindowEvent we)
frame.setSize(new Dimension(150,100));;
Get back in case of any problem.

Regards,<br />vikalps
I agree. Here's the link:
subject: JTextFiled and Jpopupmenu
jQuery in Action, 3rd edition