• 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
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

How to keep formatting while reading files

 
Ranch Hand
Posts: 492
Firefox Browser VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm trying to read a .java file into a JTextArea and no matter what method I use to read in the file the formatting is never preserved. The actual code is ok but the comments always get messed up. Here are my attempts.

Scanner:


Scanner reading the full text at once:


BufferedReader:


Is there anyway to keep the formatting the same??

PS - also posted at http://stackoverflow.com/questions/6157294/how-to-keep-formatting-while-reading-files

Hunter
 
Saloon Keeper
Posts: 9564
79
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Formatting marks exist regardless of the type of font I am using.

Hunter
 
Marshal
Posts: 27371
88
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
"Messed up"?

"Formatting marks"?

I think we need a better description of the problem. I can't tell from those phrases what the problem is. Can you show us an example?

By the way I wouldn't have written any of that code. Here's the code I would have written:
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sure, I have a .java that looks like this with comments organized the way I want them:


Then I use the readFileData() method to read this file into a JTextArea and the comments are organized the way I had them in the original file. This is what is in the JTextArea:



As you can see the comments come out not lined up in the way they were in the original file. I am referring to the block comments at line 55+.

Hunter

 
Paul Clapham
Marshal
Posts: 27371
88
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
Did you use tabs in those comments? If so, have you got the tab handling set up to work the same in the JTextPane and in wherever you created this text?
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure if I used tabs or not, that seems like a likely reason. Also I didn't know that I could specify how tabs were read. Is that a JTextPane only setting? or will I be able to do that is JTextArea, which is what I am using.

Edit: I looked at the API closer and there is a setTabSize() method in the JTextArea class. I will look into that, thanks for the help everyone.

Hunter
 
Sheriff
Posts: 22683
128
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:By the way I wouldn't have written any of that code. Here's the code I would have written:


Surely you mean this:
After all, you should always close your resources.
 
Marshal
Posts: 76452
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:Did you use tabs . . .

Yes. some of the indentation is done with tabs and some with spaces.

You could have found that out, as I did, because you could use the "edit" button to inspect the code.
 
Campbell Ritchie
Marshal
Posts: 76452
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can also copy the code from the "view plain" window onto a word-processor and use the "view non-printing characters" option.
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Spoor wrote:

Paul Clapham wrote:By the way I wouldn't have written any of that code. Here's the code I would have written:


Surely you mean this:
After all, you should always close your resources.



I had seen the read() method in the JTextComponent method list, but didn't use it immediately. The overall goal of this is for me to be able to create a text editor for .java and other files. Swing is also an area of Java where I am very weak, which is another reason I am doing this project. Thank you all for your advice. I am sure that I will have more questions as I make progress in this project.

Hunter
 
Lasagna is spaghetti flvored cake. Just like this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic