Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2005

Tip: Looking for answers? Try searching our database.

How to serialize JTable?

Thread view: 
soup_or_power@yahoo.com - 18 Nov 2005 15:46 GMT
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



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.