G'day all,
I have an application which needs to display an ASCII file of 15 lines in a Swing component that cannot be edited by the user.
Below is my code which reads the ASCII file byte by byte. My thanks to the helpful commenters who explained that a JTextArea.setEditable(false) would be appropriate in Swing.
However, my code merely displays a string of numbers, when I personally made the ASCII file to be something quite different. Does anyone know what I am doing wrong and how to get the ASCII characters themselves to display?
import java.io.*;
import javax.swing.*;
public class LoadMap extends JFrame {
public LoadMap() throws IOException {
FileInputStream fIn = new FileInputStream("map.srn");
BufferedReader rd = new BufferedReader(new InputStreamReader(fIn, "US-ASCII"));
String map = "";
JTextArea mapArea = new JTextArea(15, 50);
try {
int c;
while ((c = rd.read()) != -1) {
map= map + c;
}
} finally {
if (rd != null) {
rd.close();
}
}
mapArea.setText(map);
mapArea.setEditable(false);
add(mapArea);
pack();
setVisible(true);
}
}