When I append the data to the JTextArea in Java it does not copy the alignment.
Asked
Active
Viewed 226 times
1 Answers
1
There could be 2 problems at hand. If your original textfile is using Tabs instead of spaces to align its columns the way it does, you need to set the same tab size on your JTextArea
component.
See JavaDoc for setTabSize(int)
Secondly, alignment can only really be achieved with a mono-sapced font (where every character has the same visual width).
I just found this answer by Guillaume Polet where he describes the way to get a general-purpose monospace font by simply specifying monospaced
as the font's name:
JTextArea textArea = new JTextArea(24, 80);
textArea.setFont(new Font("monospaced", Font.PLAIN, 12));

Der_Reparator
- 86
- 1
- 9
-
I am currently using this format to write data to txt file – Newbee Nov 24 '21 at 16:38
-
String format = "%-15s %-15s %-15s"; – Newbee Nov 24 '21 at 16:38
-
it goes exactly to text file but when retrieve again and put to jtextarea the alignment is not correct. – Newbee Nov 24 '21 at 16:39
-
So you enter the text in a `JTextArea`, save it to a `.txt` file by using the above formatting String and then you load it back into the `JTextArea` but with the worng alignment? – Der_Reparator Nov 24 '21 at 16:43
-
No i put some data to the txt file first then i retrieve them again via jtextarea. – Newbee Nov 24 '21 at 16:46
-
Okay, please try out my suggested fixes and comment again if those don't fix your problem. – Der_Reparator Nov 24 '21 at 16:47
-
I write a file first then I retrieved/append them to JTextArea. – Newbee Nov 24 '21 at 16:47
-
the second one worked for me and I already upvoted your answer. thanks a lot. – Newbee Nov 24 '21 at 18:35