Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

GUI: Cut/Copy/Paste/Etc.

 
Garion Winters
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alright, yet another problem to tackle. Might not be that bad. Here is the thing-- I am having NullPointerExceptions when im trying to get the cut/copy/paste methods to work for a JTextArea.
I have buttons made for each, and an Action listener, like so...

Now, my m_textArea (is on screen and all) is what I take is causing the error. Is there something you have to do to make the methods work besides just call em like that? New to gui, so just wondering. Thanks a lot! You people are great
 
matt hooker
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Garion,
I'm afraid its not quite as easy as that (though its not too difficult either)
You need to define a clipboard object, either local or System, and to implement the ClipboardOwner interface .. among other things.
A good example of using cut/copy/paste is available here : JavaWorld - cut/copy and paste
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matt - No he doesn't. Not for simply cut/copy/pasting text.
Garion - The only thing I can possibly derive from your NullPointerException, since you failed to tell us anything else about the error, is that m_textArea has not been instantiated yet? I don't know. Could you provide more of your code possbily, or at the very least, the exact NullPointerException error. It should tell you which component you are getting that error on.
 
Garion Winters
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
*smacks himself* Yes, I guess telling you what is really wrong would help. Sorry, I was a bit sleepy. Anywhere, first off, here is my entire code...

Well, this is without the copy/paste/cut... I had worked on it a while and was working on Save. Seems to be the same problem. The actual NullPointer occurs at
String fileText = m_textArea.getText();
I think it basically does everytime I try to getText or write using m_textArea, but I'm not sure why. Just let me know what you think.
 
Bill Liteplo
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It took me a while to find this one, too.
Change this:

to this:

Essentially, you were redeclaring a local variable called m_textArea in the constructor of SimplePad which was getting instantiated.
However, your member m_textArea was not.
 
Bill Liteplo
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It took me a while to find this one, too.
Change this:

to this:

Essentially, you were redeclaring a local variable called m_textArea in the constructor of SimplePad which was getting instantiated.
However, your member m_textArea was not.
 
Garion Winters
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow. I can't believe I didn't catch that. Thanks a lot, Bill. I'm sure that was my problem with all the other areas as well. Sometimes we just have to remember the basics. Thanks again.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic