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

Thread: Changing JTable to Text and Add NewLine Between Each Column and Row

  1. #1
    Junior Member
    Join Date
    Jul 2019
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Changing JTable to Text and Add NewLine Between Each Column and Row

    I am trying to add lines or text between columns ONLY before it prints out so the text file is has each column on one line of the text file.
    The table prints out rows and than spaces, but I want the columns to have added text only when writing also have a added line.
    Example:
    <bufferedline>
    (Name: /<BufferedText>)
    TableCellName
    (Name: /<BufferedText>)
    <bufferedline>
    (Number: /<BufferedText>)
    TableCellColumnNumber
    (Number: /<BufferedText>)
    <bufferedline>
    etc.... (Total of 7)

    The code I use prints out row by row and splits with ______
    but I want to modify this code to be able to print out text before and after certain column cells.
    Appreciate any help. Super new I feel like it should be easy but can't figure it out, I understand if this is confusing, it is the best way I can describe how I need help.

    ___________Code I Use___________________________
    btnWrite.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
    try{
    File file = new File("Text.txt");
    if(!file.exists()){
    file.createNewFile();
    }

    FileWriter fw = new FileWriter(file.getAbsoluteFile());
    BufferedWriter bw = new BufferedWriter(fw);
    for(int i = 0; i < table.getRowCount(); i++){
    for(int j = 0; j < table.getColumnCount(); j++){
    bw.write(table.getModel().getValueAt(i, j)+" ");

    }
    bw.write("\n_________\n");
    }
    bw.close();
    fw.close();

    }catch(Exception ex){
    ex.printStackTrace();
    }
    }
    });

    }
    ____________________________________________
    No error codes in this code, but I want to modify to have bw write things before and after j(Column) + Lines to split information on a text in a format I need.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Changing JTable to Text and Add NewLine Between Each Column and Row

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.

    print out text before and after certain column cells.
    How will those certain cells be detected? There needs to be an if statement test to detect them and control what is printed.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 2
    Last Post: December 13th, 2013, 12:01 AM
  2. Beginniner - Add Row to JTable
    By steme in forum Java Theory & Questions
    Replies: 0
    Last Post: June 8th, 2013, 02:59 PM
  3. How do you add a value to a 2-d array by row number and column number?
    By JohnEliot in forum Java Theory & Questions
    Replies: 1
    Last Post: April 14th, 2013, 06:39 AM
  4. Add or remove a row on a jtable with Netbeans 6.0
    By Pieter in forum Java Theory & Questions
    Replies: 1
    Last Post: July 8th, 2010, 02:40 PM
  5. Highlighting both row and column in JTable
    By bschneider14 in forum AWT / Java Swing
    Replies: 4
    Last Post: May 29th, 2010, 09:14 AM

Tags for this Thread