• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to keep formatting while reading files

 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • 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
 
Carey Brown
Bartender
Pie
Posts: 1640
22
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use a fixed width font.
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Formatting marks exist regardless of the type of font I am using.

Hunter
 
Paul Clapham
Sheriff
Posts: 21322
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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 Linux VI Editor
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 21322
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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 Linux VI Editor
  • Mark post as helpful
  • send pies
  • 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
 
Rob Spoor
Sheriff
Pie
Posts: 20611
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49861
70
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Pie
Posts: 49861
70
  • Mark post as helpful
  • send pies
  • 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 Linux VI Editor
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic