开发者

Writing contents of a JTable to a file

开发者 https://www.devze.com 2023-03-05 12:09 出处:网络
I cannot seem to manage to complete this functionality. I need this to write out to a text file. My JTable is populate开发者_如何学God when a query is run, the query results then populate the JTable.

I cannot seem to manage to complete this functionality. I need this to write out to a text file.

My JTable is populate开发者_如何学God when a query is run, the query results then populate the JTable. Now with the results users of the program have the option of writing the contents of the JTable to a text file.

The output file is to have the column headings running along the top of the text file then the returned query data underneath it.


Edit: Getting Header values added part of the code

Other than writing to a file, you can use the following..

StringBuffer buffer = new StringBuffer();
int row,column,header;

// Write Header
for(header=0;header<JTable.getColumnCount;header++)
{
    //buffer.append(JTable.getColumnModel().getColumn(header).getHeaderValue(); -- This is not needed
    buffer.append(JTable.getColumnName(header);
    buffer.append(',');
}
buffer.append('\n');
// Write cell data
for(row=0;row<JTable.getRowCount();row++)
{
    for(column=0; column< JTable.getColumnCount();column++)
    {
        buffer.append(JTable.getModel().getValueAt(row, column));
        buffer.append(',');
    }
    buffer.append('\n');
}

// Write the buffer to a file
0

精彩评论

暂无评论...
验证码 换一张
取 消