This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
You just gave it a name - you still need to assign an action to it.
Try something similar to this:
The explanation: 1) only perform this code if jPopupMenu is null. If it isn't, you've already executed all of this code, and you can just return the already created popup menu.
2) I'm using the Action interface with an anonymous subclass of AbstractAction. Look that up on this forum or Google if you're unfamiliar with anonymous classes. Also note that I'm using DefaultEditorKit.copyAction. I can do this without the "new" part because the field is static.
3) I'm creating the JMenuItem using the Action object. This will set the text, any mnemonic, icon etc, as well as the action to execute.
4) The code to execute will take a reference to your text component (I called it text) and call its "copy" method. The text component will do the rest for you.
Add similar parts (from Action to the adding) for your other actions (cut, paste). Please note that usually, the order is cut, copy, paste.
Now for the showing; this code only creates and returns the JPopupMenu. You will still need to call its show method, probably in a mouse listener:
Thanks for your help , i made the copy & cut , they worked great , but the popup menu is appearing away from the form & i tried to set the X, Y but nothing happened !!! by the way what about the paste option i need to paste anything was copied from the clipboard so i think it's tactic will be diffrent , any help in this?