Skip to content
Advertisement

How to display data to a JtextArea in java with exact text alignment same from the txt file

This is the exact alignment from the text file

When I append the data to the JTextArea in Java it does not copy the alignment.

This is the image of the text area and the alignment is very different from the txt file.

Advertisement

Answer

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).

See JavaDoc for setFont(Font)

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));
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement