• Post Reply Bookmark Topic Watch Topic
  • New Topic

opening file to textfield  RSS feed

 
Candy Bortniker
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to open a file and have it appear in a scrollable textfield. I am able to open a file and I have a textfield that is scrollable. What I don't know how to do is put the file in the textfield. Any suggestions?
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am assuming that you are speaking of a text file. And you want the contents of the text file to appear in the TextField.
What you need to do is take your File object and use a BufferedReader of some flavor to read each line of the textfile into a StringBuffer object. Once you reach the EOF, myTextField.setText(myStringBuffer.toString());
Or, you could use the append() method of the textfield and each time you read a line do a myTextField.append(myLineOfTextString);
If this is not what you were talking about, please clarify.
 
Candy Bortniker
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is exactly what I was talking about. Thanks for the help.
 
Candy Bortniker
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess I need more help. This is what I have now:

I'm getting a compile error for the StringBuffer line because of the (String buff) but the API shows that as a valid constructor.
What did I do wrong?
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, the API can be a bit confusing. At least it was for me when I first started. Anytime you see something like:
StringBuffer(String arg)
Or the like that means that there is a StringBuffer constructor that takes a String as an argument. So in your code:
sb = new StringBuffer(String buff);
The problem is 2 fold
1. if buff was a String you would simply say:
sb = new StringBuffer(buff);
2. But buff is a BufferedInputStream and a StringBuffer won't accept that as an argument.
I am not sure if you are leaving part of your code out on purpose or if what you provided is all you have, but you still have a lot of work to do.
Look at the following:

Hopefully that will get you going. For future reference, when you are having code problems post at the least majority of the code you are having a problem with, because where you think the problem is could be that something else we don't see is causing it. Also, post "exact" compile/error messages. That way we know what errors we are dealing with versus: "I got errors..."
Let me know how it goes....
 
Candy Bortniker
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm now getting a 2 compile errors. One for the in.readLine() - cannot resolve symbol method readLine(), the other error is at sb.toString() - cannot resolve symbol variable sb.
Here is more of the code if that will help
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!