Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: File Text displays jumbled in JList

  1. #1
    Junior Member
    Join Date
    Mar 2010
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

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


    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.
    jumbledtext.png


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2010
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Mar 2010
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: File Text displays jumbled in JList

    Got it. Works great now. Thanks.

  6. #6
    Junior Member
    Join Date
    Mar 2010
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Beginner I/O Help: Writing To a Text File At the End of Existing Lines of Text
    By BloomingNutria in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: February 28th, 2012, 03:03 PM
  2. Listing file names in a JList
    By KILL3RTACO in forum File I/O & Other I/O Streams
    Replies: 6
    Last Post: October 8th, 2011, 12:52 PM
  3. Replies: 8
    Last Post: March 25th, 2011, 02:34 PM
  4. use multiple gui displays
    By jonwymore in forum AWT / Java Swing
    Replies: 1
    Last Post: November 19th, 2010, 10:11 AM
  5. java program to copy a text file to onother text file
    By francoc in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: April 23rd, 2010, 03:10 PM