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,
The algorithm is quite simple: