I am wondering about how to save the columns after the user reorders
them.
Thanks for your response in anticipation.
zero - 18 Nov 2005 19:00 GMT
soup_or_power@yahoo.com wrote in news:1132328791.584663.231080
@g43g2000cwa.googlegroups.com:
> I am wondering about how to save the columns after the user reorders
> them.
>
> Thanks for your response in anticipation.
It all depends on how you keep the data in Memory. Usually you don't want
to serialize the JTable, only the data. You could serialize the
TableModel. However, you probably have some other representation of the
data anyway, so it would be better to serialize that.
If you don't have a custom TableModel, and don't have the data in memory
other than in the default TableModel, you can do this:
<code>
JTable myTable;
// ...
// if you don't create your own TableModel
// you're working with a DefaultTableModel,
// which is Serializable
DefaultTableModel model = (DefaultTableModel)(myTable.getModel());
// ...
File file = new File("filename");
FileOutputStream fileOut = new FileOutputScream(file)
ObjectOutputStream output = new ObjectOutputStream(fileOut);
output.writeObject(model);
</code>
Of course you'll have to handle any I/O exceptions.
Kari Ikonen - 19 Nov 2005 12:50 GMT
> I am wondering about how to save the columns after the user reorders
> them.
If you mean saving of column order (+ potentially their widths?), then
rather good approach is to use modelIndexes of columns for persistency.
I.e. store
- Array of (ModelIndex) to save column order
- map of (ModelIndex, width) to save column widths
This info can be trivially serialized into text format e.g. like this
[modelIndex:width,]+
e.g. if model uses indexes 123, 404, 656 for columns storage can look
like this:
404:100,123:25,626:120
------
TableColumnModel columnModel = pTable.getcolumnModel();
for (TableColumn column : columnModel.getColumns()) {
Integer modelIndex = new Integer(colunn.getModelIndex());
sizes.put(modelIndex, column.getPreferredWidth());
order.add(modelIndex);
}

Signature
KI