• Post Reply Bookmark Topic Watch Topic
  • New Topic

fix reading only the last line of the file?  RSS feed

 
naved momin
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is where i m reading from a file and putting all the contents of that file in my jtextpane just like open menuitem in the menubar in normal texteditor
but the problem here is my code only takes the last line of the file means
suppose the file is
hi
how
are
you
then my code will display only the last line ie "you" in my jtextpane so i have tested my while loop condition but loop is working fine , like in above case the while loop will print 4 times hi in the console if you notice in my code because there is only 4 line in the file
please help me to fix this asap

 
Greg Charles
Sheriff
Posts: 3015
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Without even looking at the Javadoc for javax.swing.JTextPane, I can guess that setText() overwrites what is currently in the text pane with the new String you pass it. That means, you are setting the text pane to contain, "hi" on the first run through the loop, overwriting "hi" with "how" on the second run, then "how" with "are", etc. There's no addText() method in JTextPane, so you could combine calls to getText() with String concantenation, like: pane.setText(pane.getText() + s);

Better yet, you could concatenate all the text using a StringBuilder, then set it all to the text pane after the loop. Also note that readLine() takes away line separators, so you'll have to add those back in manually, or add white space. Otherwise you'll get "hihowareyou".
 
naved momin
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greg Charles wrote:Without even looking at the Javadoc for javax.swing.JTextPane, I can guess that setText() overwrites what is currently in the text pane with the new String you pass it. That means, you are setting the text pane to contain, "hi" on the first run through the loop, overwriting "hi" with "how" on the second run, then "how" with "are", etc. There's no addText() method in JTextPane, so you could combine calls to getText() with String concantenation, like: pane.setText(pane.getText() + s);

Better yet, you could concatenate all the text using a StringBuilder, then set it all to the text pane after the loop. Also note that readLine() takes away line separators, so you'll have to add those back in manually, or add white space. Otherwise you'll get "hihowareyou".

after doing this it puts all the text from the file into jtextpane but the problem is it does not copy the content as it was in the file
how to do that ...can you show me here ?
 
naved momin
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
solved with this
 
Paul Clapham
Sheriff
Posts: 22701
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greg Charles wrote:Also note that readLine() takes away line separators, so you'll have to add those back in manually, or add white space. Otherwise you'll get "hihowareyou".


You didn't say what your problem was, but it sounds very much like you had the problem that Greg Charles predicted. And told you how to solve.

Since you're a beginning Java programmer, you should keep working on it until you get it to work right. But I have to tell you, if I assigned you to do this and you brought me that code, or hopefully a working version, I would say "I hope this was a useful learning experience for you" and then rip out most of it and replace it by this code:

Yes, there's a built-in method to do that. But don't use it until you get your code working; it will be a useful learning experience.
 
Greg Charles
Sheriff
Posts: 3015
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You didn't read my last sentence, huh?

I'm glad you got it working though!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!