• Post Reply Bookmark Topic Watch Topic
  • New Topic

Converting JTextArea to String

 
Adrian Lukaszek
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello again. Have a problem with converting JTextArea to String this time. Tried .getText method and .toString but both of them don't seem to work. I also tried to declare JTextArea as non local variable but result is still the same. Error says "Cannot refer to the non final local variable textArea defined in an enclosing scope". Thanks in advance.
 
Les Morgan
Rancher
Posts: 717
17
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is how to read from the JTextArea, but converting the JTextArea object to a String is an entirely different thing altogether.

Is this what you wanted, or do you want to convert it to a String like MyObject.toString()?
 
Adrian Lukaszek
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wanted to convert it to a String like MyObject.toString().
 
Les Morgan
Rancher
Posts: 717
17
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here you go:

Adrian Lukaszek wrote:I wanted to convert it to a String like MyObject.toString().
 
Les Morgan
Rancher
Posts: 717
17
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Campbell Ritchie
Marshal
Posts: 52574
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Les Morgan wrote:
Of course, the first line is redundant. If you simply call this:-
System.out.println(myTextField);
you get exactly the same as using the toString method (unless the reference points to null). You can find the details if look look for System.out but you will have to click about six more links before you reach the end of your trail.

I am quite sure that calling toString on a text field will not give you what you are looking for.
 
Fred Kleinschmidt
Bartender
Posts: 455
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The first code in your post will work fine if you pay attention to the error message. The first line should be:

You need to declare textArea as "final" in order for the inner class ActionListener's actionPreformed() method to see it.
 
Campbell Ritchie
Marshal
Posts: 52574
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The requirement about final is slightly less strict in Java8.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!