I want to display a JTable that display the data from a DataBase table as it is.
Up till now, I have used JTable that displays data from Object [ ][ ].
I know one way to display the data is to first convert the database table into Object [ ][ ] but Is there any other which is easy yet more powerful and flexible.
I would recommend taking the following approach:
Rowclass to represent a row read from yourResultSet. This could be a simple wrapper around anObject[].List<Row>collection, and subclassAbstractTableModelto be backed by this collection.SwingWorkerto populate yourList<Row>by reading from the underlyingResultSeton a background thread (i.e. within thedoInBackground()method). CallSwingWorker‘spublishmethod to publishRows back to the Event Dispatch thread (e.g. every 100 rows).SwingWorker‘sprocessmethod is called with the latest chunk of Rows read, add them to yourList<Row>and fire appropriateTableEvents to cause the display to update.ResultSetMetaDatato determine theClassof each column within theTableModeldefinition. This will cause them to be rendered correctly (which won’t be the case if you simply use a 2DObject[][]array).The advantage of this approach is that the UI will not lock up when processing large
ResultSets, and that the display will update incrementally as results are processed.EDIT
Added example code below: