Reputation: 285
I am working on a simple file reader. It reads the .txt file then formats the output and displayed the output in a JTextArea. For some reason, the output does not display correctly. I have given my current code, followed by the text file contents below.
Code
public static JTextArea display = new JTextArea();
public static void main(String[] args) {
// GUI
JFrame frame = new JFrame("Haberdasher");
frame.setSize(450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
JPanel container = new JPanel();
container.setLayout(null);
frame.setContentPane(container);
JScrollPane scroll = new JScrollPane(display);
scroll.setBounds(10, 10, 415, 150);
container.add(scroll);
frame.toFront();
frame.setVisible(true);
// Logic
String path = "src//employees.txt";
boolean endOfFile = false;
String output = "Name" + "\t\t" + "Weekly Sales" + "\t\t" + "Weekly Pay" + "\n";
try {
FileReader fr = new FileReader(path);
BufferedReader br = new BufferedReader(fr);
while (!endOfFile) {
String name = br.readLine();
if(name == null) {
endOfFile = true;
} else {
int sale = Integer.parseInt(br.readLine());
if(name.length() >= 16) {
output += name + "\t" + sale + "\t\t" + "300" + "\n";
} else {
output += name + "\t\t" + sale + "\t\t" + "300" + "\n";
}
}
}
br.close();
System.out.println(output);
display.setText(output);
} catch (IOException e) {
System.out.println(e);
}
}
employees.txt Contents: http://hastebin.com/ijuyedizil.nginx
Expected Output: http://hastebin.com/epesipatot.nginx
Upvotes: 0
Views: 308
Reputation: 324108
Now, the output is fine in the console, but not in the JTextArea.
If you want the text to align like it does on the console need to use a monospaced font
textArea.setFone( new Font("monospaced", Font.PLAIN, 10) );
You may also need to use:
textArea.setTabSize(...);
Upvotes: 1