aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Copy and paste popup menu Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Copy and paste popup menu" Watch "Copy and paste popup menu" New topic
Author

Copy and paste popup menu

Sherif Shehab
Ranch Hand

Joined: Mar 05, 2007
Posts: 483

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 ?

Thanks


Thanks,
Sherif
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19760
    
  20

Do you know how to create a popup menu? If not, check the API page for javax.swing.JPopupMenu. There is a link to the tutorial there.

javax.swing.text.JTextComponent has three methods you can use: cut(), copy() and paste(). Guess what they do
JTextField, JTextArea and all other (direct and indirect) subclasses inherit these.

As for JLabel, you can only use getText() and setText(String). You might want to check the Drag&Drop Tutorial, especially CCP in a non-Text Component.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Sherif Shehab
Ranch Hand

Joined: Mar 05, 2007
Posts: 483

hi
i made all what you said and nothing happened even the menu doesnt want to appear !! excuse my poor knowledge in swing

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19760
    
  20

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:
Sherif Shehab
Ranch Hand

Joined: Mar 05, 2007
Posts: 483

hi ,

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?

Thanks
Sherif Shehab
Ranch Hand

Joined: Mar 05, 2007
Posts: 483

hi
i fixed the x,y problem but i cant fix the paste issue...
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19760
    
  20

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?
Sherif Shehab
Ranch Hand

Joined: Mar 05, 2007
Posts: 483

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?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19760
    
  20

Yes, if it contains text. Otherwise it will do nothing.
Rusty Who
Greenhorn

Joined: Jun 01, 2012
Posts: 1
I can't believe none of you can properly make use of built-in DefaultEditorKit actions.

The code you need is:

Here's a complete example method that will install a Copy/Cut/Paste context menu to all editable components in a given container:
 
 
subject: Copy and paste popup menu