• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Cut,copy and paste

 
Jennifer Sohl
Ranch Hand
Posts: 455
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to get my app to cut, copy and paste, but can't seem to make
it work.
I want to be able to figure out what component has focus, and then get the text from that component and copy it to the clipboard.
Here is my code to copy :

What am I doing wrong here?
No matter what I do, it seems like it's not seeing the components on the JFrame. When I print the component to stdout , it always prints out javax.swing.JRootPane...........
Can someone please enlighten me?
Thanks a million!
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is this a SWING app? I think you might be approaching with too much complexity.
All the Text classes in SWING have a cut(), copy(), and paste() method. I usually build a ContextMenu (JPopupMenu) and when I right click on the highlighted text, that menu pops up and on the appropriate menu item, all you have to do is call one of those methods. If this is along the lines of what you are trying to do, I will try and drudge up some code where I have done that. If it isn't let me know, and we will see if we can go about it a different way that works for you.
 
Jennifer Sohl
Ranch Hand
Posts: 455
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, this is a swing app. Right now I am trying to put the cut, copy and paste methods into a custom menu bar class and get the focus of the component for the active window. Then if that component is a text component, it does the appropriate cut,copy or paste method. But obviously, I can't get that to work.
I do also need to do this with a JPopupMenu as well.
If you have some example code on how to do the popup menu , as well as do cut,copy,paste with a menu bar, I would be forever grateful.
Thanks for your help, it is MUCH appreciated!!
 
Jennifer Sohl
Ranch Hand
Posts: 455
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone please tell me when using JMenu Cut,Copy and paste, how it knows what text component to do the action on? If I used getFocusOwner(), it seems that the JMenu just clicked has the focus, correct?
I just can't seem to figure out how this works. I've been searching and can't find any good examples that use more than one text component.
Thanks again for any help!
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, here is how I did it with a JPopupMenu:

The code above is a mouselistener which I register on all my text objects. triggerComponent is a public JTextComponent object in the parent class (this is a subclass). textCommandPopupMenu is the JPopupMenu object.
Now here is my JPopupMenu code:

Now, as per your delima where you are doing this from a JMenuBar, what you probably will want to do is add a focusListener that is specificly for your Text objects. And have another variable similar to my triggerComponent that when the Text object gains focus, it is set to this variable. Or something along those lines. I believe you are correct about getFocusOwner changing to the Menu when are selecting items in the Menu.
 
Jennifer Sohl
Ranch Hand
Posts: 455
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gregg,
Thanks for the help on cut, copy and paste. The example you showed me
helps out tremendously. As far as getting the cut, copy and paste to work from my menu bar, I took your advice, and got it to work.
Thank you so much!!
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jennifer Sohl:
Gregg,
Thanks for the help on cut, copy and paste. The example you showed me
helps out tremendously. As far as getting the cut, copy and paste to work from my menu bar, I took your advice, and got it to work.
Thank you so much!!

Glad I could help.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic