This week's book giveaway is in the JDBC and Relational Databases forum.
We're giving away four copies of Resilient Oracle PL/SQL: Building Resilient Database Solutions for Continuous Operation and have Stephen Morris on-line!
See this thread for details.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Peter Rooke
  • Himai Minh
Bartenders:
  • Piet Souris
  • Mikalai Zaikin

opening file to textfield

 
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
He does not suffer fools gladly. But this tiny ad does:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic