Hi pals, I need to make a popup menu contains copy and paste in a swing to copy or paste from jlabel or jtextbox , but i dont know how so anyone can help me ?
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?
Originally posted by Rob Prime: Text components can only have text pasted. It will paste it at the current cursor location if you call paste(). Is that not good enough?
OK that's good but from where "Paste" will get the stuff that will paste ? from the clipboard?