Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I had a very similar problem, and I think my result was similar too, except it didn't need reflection (static DB schema). You need to create row objects for each row (which may just include row number and references to a ResultSet and column info). </p> <p>Then write a <code>ca.odell.glazedlists.gui.WritableTableFormat</code> implementation to map these objects to table cells. </p> <p>To avoid problems with #2, you can create a flexible row class that fetches column info once from the ResultSet and caches it for reuse. </p> <p>Edit: I found an original and simpler implementation (fairly simple) that mine was based upon. You can view it here: <a href="http://www.java2s.com/Code/Java/Swing-JFC/ResultSetTable.htm" rel="nofollow noreferrer">ResultSet Table</a>. It might be sufficient for your purposes. Then you add this to the AbstractTableModel implementation provided by the link.</p> <pre><code>public void setValueAt(Object ob, int row, int column) throws SQLException { resultSet.absolute(r+1); if (ob == null) { resultSet.updateNull(column+2); } else { resultSet.updateObject(column+2,ob); } rs.updateRow(); this.fireTableCellUpdated(row,column); } public boolean isCellEditable(int row, int col) { return true; } </code></pre> <p>There are three catches though: your ResultSet needs to be updatable, support scrolling both directions, and be sensitive to updates to the DB. These are part of the JDBC spec, but not all drivers support them, and you need to make sure your ResultSet is created with them enabled. In that case, you just do <code>this.fireTableDataChanged()</code> periodically to force a full update of the table data. It's not the fastest approach, but it does work.</p> <hr> <h2>Edit2: Another approach</h2> <p>What about using one of the Object-relational mapper libraries, and then do the <code>ca.odell.glazedlists.gui.WritableTableFormat</code> like I suggested above?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload