akshayc
akshayc

Reputation: 484

How do I write the contents of a JTable to a text file?

I have been working on a side project in which I have a JTable and need to write the contents of it to a text file. It is part of a JFrame in which there is an "update" button, which the user is supposed to click to update the text file. However, when I invoke the method to update the spreadsheet, all it creates is a blank file.

Here is the table model:

    spreadsheet.setModel(new javax.swing.table.DefaultTableModel(
        new Object [][] {
            {"", "0", false, false},
            {"", "0", false, false},
            {"", "0", false, false},
            {"", "0", false, false},
            {"", "0", false, false},
            {"", "0", false, false},
            {"", "0", false, false},
            {"", "0", false, false},
            {"", "0", false, false},
            {"", "0", false, false},
            {"", "0", false, false},
            {"", "0", false, false},
            {"", "0", false, false},
            {"", "0", false, false},
            {"", "0", false, false},
            {"", "0", false, false},
            {"", "0", false, false},
            {"", "0", false, false},
            {"", "0", false, false},
            {"", "0", false, false},
            {"", "0", false, false},
            {"", "0", false, false},
            {"", "0", false, false},
            {"", "0", false, false},
            {"", "0", false, false},
            {"", "0", false, false},
            {"", "0", false, false},
            {"", "0", false, false},
            {"", "0", false, false},
            {"", "0", false, false}
        },
        new String [] {
            "Descripton", "Amount", "Income?", "Expense?"
        }
    ) {
        Class[] types = new Class [] {
            java.lang.String.class, java.lang.Integer.class, java.lang.Boolean.class, java.lang.Boolean.class
        };

        public Class getColumnClass(int columnIndex) {
            return types [columnIndex];
        }
    });
    jScrollPane1.setViewportView(spreadsheet);

And here is the method that converts the table to a text file:

private void updtSSActionPerformed(java.awt.event.ActionEvent evt) {                                       
    try {
        StringBuffer fileContent = new StringBuffer();
        TableModel tModel = spreadsheet.getModel();
        for (int i = 0; i < tModel.getRowCount(); i++) {

            Object cellValue = tModel.getValueAt(i, 0);
            fileContent.append(cellValue);
        }

        FileWriter fileWriter = new FileWriter(new File("data.txt"));
        fileWriter.write(fileContent.toString());
        fileWriter.flush();
        fileWriter.close();
    } catch (IOException ex) {
        Logger.getLogger(Spreadsheet.class.getName()).log(Level.SEVERE, null, ex);
    }
}

(I obtained the above method from here: https://community.oracle.com/message/5235252)

How do I fix this? I am still somewhat new to Java, so any suggestions are greatly appreciated.

Upvotes: 0

Views: 270

Answers (1)

Ben
Ben

Reputation: 44

I think the problem might be that you are only getting the value of the first column in your table model, and this seems to be empty Strings. You would need to traverse every column for every row in your table model to create e.g. a String or StringBuffer that you could then write to your file.

You can call getColumnCount() on your table model to get the number of columns, then in the for loop you already created, do another loop where you traverse the columns for each row, assemble a String or StringBuffer that you append to your fileContent StringBuffer, and you might want to put a \\n as well after each row.

If the table model gets larger you might want to consider writing to a file on the fly instead of assembling in memory with a StringBuffer.

Upvotes: 1

Related Questions