Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I went back and had a better look at this after seeing kleopatra's comment. I was changing my TableModel after creating a RowSorter, but before attaching the RowSorter to the JTable. Here's an example that shows the problem I was having.</p> <pre><code>import javax.swing.*; import javax.swing.table.AbstractTableModel; import javax.swing.table.TableRowSorter; import java.util.ArrayList; import java.util.List; public class TestTableMain { public static void main(String[] args) { new TestTableMain(); } public TestTableMain() { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { buildAndShowMainFrame(); } }); } private void buildAndShowMainFrame() { JFrame frame = new JFrame(); JScrollPane scrollPane = new JScrollPane(); TestTableModel model = new TestTableModel(); JTable table = new JTable(model); TableRowSorter&lt;TestTableModel&gt; rowSorter = new TableRowSorter&lt;&gt;(model); rowSorter.setRowFilter(null); model.add("First added item."); /* The RowSorter doesn't observe the TableModel directly. Instead, * the JTable observes the TableModel and notifies the RowSorter * about changes. At this point, the RowSorter(s) internal variable * modelRowCount is incorrect. There are two easy ways to fix this: * * 1. Don't add data to the model until the RowSorter has been * attached to the JTable. * * 2. Notify the RowSorter about model changes just prior to * attaching it to the JTable. */ // Uncomment the next line to notify rowSorter that you've changed // the model it's using prior to attaching it to the table. //rowSorter.modelStructureChanged(); table.setRowSorter(rowSorter); scrollPane.setViewportView(table); frame.setContentPane(scrollPane); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); frame.pack(); frame.setVisible(true); model.add("Second added item."); } private class TestTableModel extends AbstractTableModel { private List&lt;String&gt; items = new ArrayList&lt;&gt;(); public TestTableModel() { for(int i=0;i&lt;5;i++) { add("Item " + i); } } @Override public int getRowCount() { return items.size(); } @Override public int getColumnCount() { return 1; } @Override public Object getValueAt(int rowIndex, int columnIndex) { return items.get(rowIndex); } public void add(String item) { items.add(item); fireTableRowsInserted(items.size() - 1, items.size() - 1); } } } </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