Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Copy and paste popup menu

 
Sherif Shehab
Ranch Hand
Posts: 485
Android Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sherif Shehab
Ranch Hand
Posts: 485
Android Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 485
Android Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 485
Android Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
i fixed the x,y problem but i cant fix the paste issue...
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 485
Android Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, if it contains text. Otherwise it will do nothing.
 
Rusty Who
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
prithiraj sengupta
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code below doesn't behave same through all OS. The programmer must check it as per the Java documentation.

So as an example, here is a replacement to ensure the popup is executed only on right click.


Also, as per your requirements you can implement more such methods from SwingUtilities,
For left click:


enjoy coding
 
Campbell Ritchie
Sheriff
Posts: 48386
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic