So, here's the scenario: I have a basic JTextField in a frame and want to give the user the option, to right click in the textfield and then, like in Eclipse or Microsoft Word, give him in a popup-menu the option to copy the text or paste text he already has created. How do I make this special kind of right click event?
That's a short version of the program, I have so far:
import java.awt.*;
import javax.swing.*;
public class TestClass extends JFrame{
private JFrame frame;
private JTextField textField;
/**
* Main method
* @param args
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
TestClass window = new TestClass();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the window
*/
public TestClass() {
initialize();
}
/**
* Initialize components (TextField)
*/
private void initialize() {
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(100, 100, 200, 100);
textField = new JTextField();
textField.setText("TextField");
textField.setFont(new Font("Arial", Font.PLAIN, 20));
frame.add(textField);
}
}
You should use Mouse Event to register an event for you then you must use a popupmenu to be popped up when you click so far here is an example code for you!
privatevoidformMouseClicked(java.awt.event.MouseEventevt){
if (evt.isPopupTrigger()){
pop.show(evt.getComponent(),evt.getX(), evt.getY());
}
}