1 Attachment(s)
File Text displays jumbled in JList
Hello,
I have a text file which I am reading into a JList. The file is being read in but it is displaying jumbled characters. I would just like to verify that there is nothing I am doing wrong below that would lead to this issue.
Code :
try{
// Populate the list box with the descr from the file.
FileReader fr = new FileReader(fileName);
BufferedReader br = new BufferedReader(fr);
Vector<String> lines = new Vector<String>();
String buffer;
while ((buffer=br.readLine())!=null){
lines.add(buffer);
}
JList trackListBox= new JList(lines);
JScrollPane tracksScrollPane = new JScrollPane(trackListBox);
TitledBorder listBorder = BorderFactory.createTitledBorder("Description");
tracksScrollPane.setBorder(listBorder);
add(BorderLayout.CENTER, tracksScrollPane);
}
catch(Exception e){
e.printStackTrace();
}
Sample Image of what is being displayed.
Attachment 1210
Re: File Text displays jumbled in JList
What is the internal format of the file that you are reading?
Look at the file in a HexEditor to see what is in it. What do you see if you open the file in a simple editor?
Re: File Text displays jumbled in JList
It is just a word doc with a paragraph of english I tried both doc and docx formats and produced the same results both times.
Re: File Text displays jumbled in JList
A doc file is not a text file that can be read line by line in a Java program. You need special code that understands its internal format to get "lines" of text from it.
Save the file as a .txt file.
Re: File Text displays jumbled in JList
Got it. Works great now. Thanks.
Re: File Text displays jumbled in JList
Ok, so I have another question. Currently the program is printing the entire text paragraph onto one line in the JList. How can I adjust this so that the text flows onto the next line when it reaches the edge of the JList.
PS: Forgive me for asking simple questions but I am very new to Java GUI.
Re: File Text displays jumbled in JList
Why are you putting long lines of text in a JList?
You could use one of the String class's methods to find a place to break a line and use the substring method to break the line.