Note that there are some explanatory texts on larger screens.

plurals
  1. POJDBC TableModel for a JTable in Java?
    primarykey
    data
    text
    <p>I want to display a database table as a JTable. I have never used JTable before so I googled JTable and TableModel.</p> <p>With that googling, I am able to write my own custom TableModel which show data stored in </p> <pre><code>Object[][] data; </code></pre> <p>Now, I want to show my database table data into JTable. I searched that also and have got an idea of that but still confused about what should goes where in the implementation class of AbstractTableModel.</p> <p>Following is the code of custom TableModel.</p> <pre><code>public abstract class AbstractPOLDATTableModel extends AbstractTableModel { protected boolean DEBUG = false; private String[] columnNames; private Object[][] data; protected AbstractPOLDATTableModel(String[] columnNames, Object[][] data) { this.columnNames = columnNames; this.data = data; } public int getColumnCount() { return columnNames.length; } public int getRowCount() { return data.length; } @Override public String getColumnName(int col) { return columnNames[col]; } public Object getValueAt(int row, int col) { return data[row][col]; } @Override public Class getColumnClass(int c) { return getValueAt(0, c).getClass(); } @Override public boolean isCellEditable(int row, int col) { if (col &lt; 2) { return false; } else { return true; } } @Override public void setValueAt(Object value, int row, int col) { if (DEBUG) { System.out.println("Setting value at " + row + "," + col + " to " + value + " (an instance of " + value.getClass() + ")"); } data[row][col] = value; fireTableCellUpdated(row, col); if (DEBUG) { System.out.println("New value of data:"); printDebugData(); } } private void printDebugData() { int numRows = getRowCount(); int numCols = getColumnCount(); for (int i=0; i &lt; numRows; i++) { System.out.print(" row " + i + ":"); for (int j=0; j &lt; numCols; j++) { System.out.print(" " + data[i][j]); } System.out.println(); } System.out.println("--------------------------"); } } </code></pre> <p>Now, how to change the above code so that my JTable can have the follwing features:</p> <ol> <li>It shows data from the database</li> <li>User can edit the table directly and when he clicks on a "save" button, the changes reflect in the database data</li> <li>User can insert data directly.</li> <li>User can delete data directly.</li> </ol>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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