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 5 of 5

Thread: Need line breaks in output display

  1. #1
    Member
    Join Date
    Jan 2013
    Posts
    37
    My Mood
    Confused
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Need line breaks in output display

    I have to get a list of entries from a text file to display in a JFrame. I was able to do that but everything is displaying on one line.

    How do I get it to display as it is listed in the text file?

    Here is what the text file contains:

    Rick Sebastian 49 Rick@home.com 813-111-2222
    John Doe 35 John@work.com 813-222-3333
    Peggy Bundy 50 Peggy@theoffice.com 813-333-4444
    Al Bundy 55 Al@thebar.com 813-444-5555
    Jane Doe 27 Jane@thebeach.com 813-555-6666

    Here is how it is displayed in the program:
    Capture.jpg

    Here is the code I'm working with:

    package contact.display.info;
     
    import java.io.*;
    import javax.swing.*;
     
    /**
     *
     * @author Rick Sebastian
     */
    public class ContactDisplayInfo extends JFrame {
     
    JTabbedPane jtp1=new JTabbedPane();
    JPanel jp1=new JPanel();
    JTextArea t1=new JTextArea();
    ContactDisplayInfo () throws Exception
     
    {
            super("ContactDisplayinfo");
            FileReader f=new FileReader("C:/Users/Me/Documents/ContactInformationProgram/ContactInfo.txt");
            BufferedReader brk=new BufferedReader(f);       
            String s;
            while((s=brk.readLine())!=null){
            t1.append(s);
            }
            jp1.add(t1);
            jtp1.addTab("Tab1",t1);
            add(jtp1);  
            //setSize(400, 400);
            setLocationRelativeTo(null);
     
        }
        public static void main(String args[]) throws Exception
        {
            ContactDisplayInfo cdi=new ContactDisplayInfo();
            cdi.pack();
            cdi.setVisible(true);   
            cdi.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        }
    }

    I set the frame size to 400 400 and that only changed the size of the frame. The text still extended out in one line. i know I need a word wrap of some sort, but I'm not sure how that's done, or how I would get it to group by individual entry. Ideally I would have 5 lines with 5 entries being displayed.

    Any help you can offer would be greatly appreciated!

    Rick


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

    Default Re: Need line breaks in output display

    How do I get it to display as it is listed in the text file?
    Append a "\n" at the end of the line where you want the following data on the next line.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Jan 2013
    Posts
    37
    My Mood
    Confused
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Need line breaks in output display

    In order to do that, I would have to modify the text file itself, no? If not, where would I put that in the code?

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

    Default Re: Need line breaks in output display

    Append the "\n" at the end of the String where you want the next String to go to the next line. The readLine() methods strips off the newline character when it reads a line from the file into a String. You need to put it back.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Feb 2013
    Posts
    45
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default Re: Need line breaks in output display

    Try this:
    If you need to print the output line by line you use this...

     while((s=brk.readLine())!=null){
            t1.append(s+"\n");
            }
    Regards
    Android developer
    Trinay Technology Solutions
    http://www.trinaytech.com
    5705750475

Similar Threads

  1. Program Breaks When In Jar Format
    By tyeeeee1 in forum What's Wrong With My Code?
    Replies: 11
    Last Post: December 9th, 2012, 02:36 PM
  2. Output doesn't display numbers
    By Sylis in forum What's Wrong With My Code?
    Replies: 6
    Last Post: November 14th, 2012, 11:51 PM
  3. Proper way to get rid of breaks.
    By Massaslayer in forum Loops & Control Statements
    Replies: 6
    Last Post: December 23rd, 2011, 07:53 PM
  4. need numbers to output onto a new line
    By oscar22 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 27th, 2011, 07:32 AM
  5. Display Servlet Output into JSP Page
    By DanBrown in forum JavaServer Pages: JSP & JSTL
    Replies: 3
    Last Post: February 28th, 2011, 10:34 AM