• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

fix reading only the last line of the file?

 
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Sheriff
Posts: 3063
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
solved with this
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 3063
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You didn't read my last sentence, huh?

I'm glad you got it working though!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic