wood burning stoves 2.0*
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTextFiled and Jpopupmenu" Watch "JTextFiled and Jpopupmenu" New topic
Author

JTextFiled and Jpopupmenu

tausahmed ahmed
Greenhorn

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
Greenhorn

Joined: May 10, 2003
Posts: 18
Hi,
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) )
{
menu.add(actions[i]);
}
}
textField.addMouseListener(new MouseAdapter()
{
public void mouseReleased(MouseEvent me)
{
if(me.isPopupTrigger())
{
menu.show(textField,me.getX(),me.getY());
}
}
});
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(2,1));
panel.add(new JLabel("Enter some text:"));
panel.add(textField);
getContentPane().add(panel);
}
public static void main(String[] args)
{
final JFrame frame = new MessagePoster();
frame.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});
frame.setSize(new Dimension(150,100));
frame.show();
}
}
/*--------------------------------------------*/
Get back in case of any problem.
Regards,
vikalp_setya@rediffmail.com


Regards,<br />vikalps
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: JTextFiled and Jpopupmenu
 
Similar Threads
Copy text from DIV
Running a jar file
Why can't JSF calls method with parameter?
regarding jq+ code
Doubt in EL [] operator