Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to read text file to JScrollPanel?  RSS feed

 
Kevin Goodwin
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am taking a class on java and am having a problem with a program. I have a ScrollPane setup in a separate class and am trying to inport a text file. I have it so it inports to the console screen, but can't figure out how to inport it to my GUI screen. I don't have much experience and have read several different books on it, but am still lost. Is it okay to post the code I have so far for clarity? Thanks.
 
Joe Ess
Bartender
Posts: 9406
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the JavaRanch, Kevin.
A ScrollPane is only useful to scroll some other component. There's several components which can be used to display text, depending on how complex a display you want. Of these, Text Area is the simplest to use. Take a look at those links and let us know if you have any other questions.
 
Kevin Goodwin
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the welcome Joe and the links. I have read many of those descriptions, but I am just not certain how to go about implementing them. Here is my code thus far. I know I need to install a statement where the System.out.println is currently at. That was just a test to see if things are working good to that point. Now I just need to learn the secret handshake to get my data from the console screen to the textarea. My mistake for identifying it as a scrollpane.

[ May 16, 2005: Message edited by: Gregg Bolinger ]
 
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, that System.out should be an append method on your text area. You've almost got it. I am wondering though why you are using a java.awt.TextArea instead of a javax.swing.JTextArea. At any rate, textArea.append(str), will probably get the job done.

Since your TextArea is a local variable it will also have to be defined as final unless you make it global. But you'll see that error if you try and compile as is when adding the append in the while loop.

HTH
 
Kevin Goodwin
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Gregg for that attempt but now I get the error I think you were eluding to. When I add TextArea.append(str); just below the System.out stuff I get this output error:

non-static method append (java.lang.String) cannot be referenced from a static context. It then lists the error line with a caret pointing at the . between TextArea.append

Also, I tried to make the TextArea a JTextArea and it errors out as well. That I really don't understand.
 
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 Kevin Goodwin:
Thanks Gregg for that attempt but now I get the error I think you were eluding to. When I add TextArea.append(str); just below the System.out stuff I get this output error:

non-static method append (java.lang.String) cannot be referenced from a static context. It then lists the error line with a caret pointing at the . between TextArea.append

Also, I tried to make the TextArea a JTextArea and it errors out as well. That I really don't understand.


Well, I'll see if I can explain some of this. When I compile your code with the append method I get the following error:


C:\ShowAllFrame.java:56: local variable scroller is accessed from within inner class; needs to be declared final
scroller.append(str);


So what you have to do to solve that is:

final TextArea scroller = new TextArea("", 7, 40, TextArea.SCROLLBARS_BOTH);

Now with that being said, the reasonm I questioned TextArea is because you have created a Swing application (JFrame, JButton, etc) but are using an AWT component. Remove the line for TextArea.... and replace it with the following:



Then instead of scroller.append do textArea.append(str).
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks like you aren't having a problem with reading the text file and that this is leaning more to GUI questions, so I am going to move it to our Swing forum.
 
Kevin Goodwin
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That did the trick. Thank you Gregg, your assistance was much appreciated. Now on to some other pesky problems with other parts of my program. I'm sure I'll be back for more help.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!