Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9088633
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T21:56:50+00:00 2026-06-16T21:56:50+00:00

I am saving a .txt and .doc file containing the data from my JTable.

  • 0

I am saving a .txt and .doc file containing the data from my JTable. At the minute when it saves it lays the text out like its in a table, but due to different lengths of data it does not fit. So I am trying to get the date to lay out as follows:

Column 1 name: row 1 column 1 data

Column 2 name: row 1 column 2 data

Column 3 name: row 1 column 3 data

Column 4 name: row 1 column 4 data

Column 1 name: row 2 column 1 data

Column 2 name: row 2 column 2 data

Column 3 name: row 2 column 3 data

Column 4 name: row 2 column 4 data

etc.

The code I have at the minute is:

private void saveResultsActionPerformed(ActionEvent evt) {


    int returnVal = fileChooser.showSaveDialog(NewJFrame.this);
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        try {
            File file = fileChooser.getSelectedFile();
            PrintWriter os = new PrintWriter(file);
            os.println("");
            for (int col = 0; col < table.getColumnCount(); col++) {
                os.print(table.getColumnName(col) + "\t");
            }

            os.println("");
            os.println("");

            for (int i = 0; i < table.getRowCount(); i++) {
                for (int j = 0; j < table.getColumnCount(); j++) {
                    os.print(table.getValueAt(i, j).toString() + "\t");

                }
                os.println("");
            }
            os.close();
            System.out.println("Done!");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

But please keep in mind that each of my tables has different number of columns and rows.
Ive tried saving the columns and data in arrays, and I have a feeling this is the right way to go around the problem but I cant figure out how to print it in the order i mentioned,

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T21:56:51+00:00Added an answer on June 16, 2026 at 9:56 pm

    The algorithm is quite simple:

    for (int row = 0; row < table.getRowCount(); row++) {
        for (int col = 0; col < table.getColumnCount(); col++) {
            os.print(table.getColumnName(col));
            os.print(": ");
            os.println(table.getValueAt(row, col));
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm making a dream dictionary, i'm reading from a txt file and saving to
I am retrieving data from the SQL database and saving it '.txt' as a
between saving a key name like this for(var key in this.data) { var key_name
I've been saving some automatically-generated performance data for my code in perf.txt. Every time
I have .txt files that contain the output from a data logger. The data
I'm trying to simulate a simple download application by copying a file like myBook.txt
I am currently saving a message to a single file from multiple activity classes
I have a file I am saving out in php that is just a
i am saving a NSMutuablearray to a text file, he has NSNmbers in it
I am facing a problem while saving a .csv file from browser. I made

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.