title: Saving JTable as text file
body: <p>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:</p>
  
  <p>Column 1 name: row 1 column 1 data</p>
  
  <p>Column 2 name: row 1 column 2 data</p>
  
  <p>Column 3 name: row 1 column 3 data</p>
  
  <p>Column 4 name: row 1 column 4 data</p>
  
  <p>Column 1 name: row 2 column 1 data</p>
  
  <p>Column 2 name: row 2 column 2 data</p>
  
  <p>Column 3 name: row 2 column 3 data</p>
  
  <p>Column 4 name: row 2 column 4 data</p>
  
  <p>etc.</p>
  
  <p>The code I have at the minute is:</p>
  
  <pre><code>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 &lt; table.getColumnCount(); col++) {
                  os.print(table.getColumnName(col) + "\t");
              }
  
              os.println("");
              os.println("");
  
              for (int i = 0; i &lt; table.getRowCount(); i++) {
                  for (int j = 0; j &lt; 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();
          }
      }
  }
  </code></pre>
  
  <p>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,</p>