Note that there are some explanatory texts on larger screens.

plurals
  1. POSaving JTable as text file
    primarykey
    data
    text
    <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>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload