This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Hi people, Is there a better way of passing data from a data source to JTable, out of using array? Cant I use ResultSet directly, instead of an array? Also cant we make all of the changes on the cells of JTable object we display, and reflect the changes directly from the cells to database? The existence of an array looks cumbersome... is there any other way? Thank you..
I don't think this is really a beginner's question. A bit of JDBC and a bit of Swing. Difficult to be sure, but you are talking about updating databases, so maybe the JDBC forum would be best for it, and I shall move you . . .
I don't see any reason why you couldn't write a TableModel that encapsulated a ResultSet. In fact I would bet somebody has already done that and posted it somewhere on the Internet. The Google keywords I would use for that are "Swing jtable resultset".
As you see I don't think this is a JDBC question. But from the JDBC point of view I don't necessarily think that keeping a ResultSet open and updating it qualifies as "better".
But I won't bother to move this (again) to the Swing forum because I don't think there's much to say beyond "yes".
Just make the ResultSet the table's model then. Extend AbstractTableModel and implement getColumnName(), getColumnCount(), getRowCount(), etc...
And include a method to set the model to a new ResultSet and fire off a fireTableDataChanged();
Good to go!
EDIT: I typically like to encapsulate my DB results into a custom Bean of some sort, rather than using the ResultSet (since I typically modify these Beans elsewhere in my app, not just display via a Table), but that's just my preference. [ July 07, 2008: Message edited by: Ted Smyth ]