Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Starting from this <a href="https://stackoverflow.com/a/8260663/230513">example</a>, I saw no unexpected behavior with the <code>getColumnClass()</code> implementation shown below.</p> <pre><code>private Model() { final Object[] data = {this.toString()}; this.model = new DefaultTableModel(data, 1){ @Override public Class&lt;?&gt; getColumnClass(int columnIndex) { System.out.println(data[0]); return super.getColumnClass(columnIndex); } }; model.addRow(data); } </code></pre> <p>Note that <code>JTable</code> may invoke <code>getColumnClass()</code> anytime it determines that a cell needs rendering. If necessary, you can use <code>EventQueue.invokeLater()</code> to schedule something that will "happen after all pending events are processed."</p> <pre><code>EventQueue.invokeLater(new Runnable() { @Override public void run() { // do something } }); </code></pre> <p>Addenda:</p> <ul> <li><p>If <code>getAutoCreateRowSorter()</code> is <code>true</code>, <code>setModel()</code> tries to restore the <code>RowSorter</code> created using the old model. </p></li> <li><p>You can specify <code>setAutoCreateRowSorter(true)</code> <em>after</em> you change models, as shown below, or extend your <code>TableModel</code> to update the model in place, as shown <a href="https://stackoverflow.com/a/11241218/230513">here</a>.</p></li> <li><p>Use <code>ActionListener</code> for <code>JButton</code> instead of <code>MouseListener</code>.</p></li> <li><p>Use a <code>TableCellEditor</code> for buttons in the table, as shown <a href="https://stackoverflow.com/a/5556631/230513">here</a> and <a href="https://stackoverflow.com/a/5559747/230513">here</a>.</p></li> </ul> <p>Code:</p> <pre><code>addButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if (table.getModel().getRowCount() == 2) { return; } Object[][] data = new Object[table.getModel().getRowCount() + 1][5]; table.setRowSorter(null); for (int i = 0; i &lt;= table.getModel().getRowCount(); i++) { data[i][0] = i; data[i][6] = "User" + i; data[i][7] = "Delete"; } table.setModel(new CustModel(data)); table.setAutoCreateRowSorter(true); } }); </code></pre>
 

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