JTable row selection after updating TableModel

How to save a series of table sections after updating the table mode (fireTableDataChanes)? I know that I have to save the selection before the fire and restore it after (from there ). But when I try to restore the selection in the TableModelListener, this will not work. So where should I restore the choice?

Update: Now I'm trying to restore the selection this way: table.setModel (model);

model.addTableModelListener(new TableModelListener() { @Override public void tableChanged(TableModelEvent e) { table.addRowSelectionInterval(1, 1); } }); 

but that will not work.

+4
source share
3 answers

The solution is simple: we must use the Swing EventQueue:

 final int selectedRow = 0; // Save selected row table tableList.getSelectionModel().addListSelectionListener(new ListSelectionListener() { @Override public void valueChanged(ListSelectionEvent e) { selectedRow = e.getFirstIndex(); } }); // Restore selected raw table model.addTableModelListener(new TableModelListener() { @Override public void tableChanged(TableModelEvent e) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { if (selectedRow >= 0) { tableList.addRowSelectionInterval(index, index); } } }); } }); 
0
source

This is an updated version of the first answer. You can use a field to store a string, because inner classes can access it. I had problems with this solution because I used a periodic thread to update the table.

 private int selectedRow = -1; public void mymethod() { //put code to create the table here... JTable tableList = new JTable(); // Save selected row table tableList.getSelectionModel().addListSelectionListener(new ListSelectionListener() { @Override public void valueChanged(ListSelectionEvent e) { selectedRow = e.getFirstIndex(); } }); // Restore selected raw table model.addTableModelListener(new TableModelListener() { @Override public void tableChanged(TableModelEvent e) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { if (selectedRow >= 0) { tableList.addRowSelectionInterval(index, index); } } }); } }); } 
+2
source

The example from the first answer shows the main solution, but will not work out of the box. Unable to change final "selectedRow". Therefore, you should use a mutable integer (for example, AtomicInteger). TamleModelListener sends the task to the swing, so it is called later - in most cases, after changing the table, the selection is already deleted. Thus, selectRow will in most cases be -1.

I slightly modified the above example (and added single cell selection, but also works for full rows):

  final AtomicInteger selectedRow=new AtomicInteger(-1); final AtomicInteger selectedCol=new AtomicInteger(-1); tblZeiten.getSelectionModel().addListSelectionListener(new ListSelectionListener() { @Override public void valueChanged(ListSelectionEvent e) { selectedRow.set(tblZeiten.getSelectedRow()); selectedCol.set(tblZeiten.getSelectedColumn()); } }); tblZeiten.getModel().addTableModelListener(new TableModelListener() { @Override public void tableChanged(TableModelEvent e) { TableCellEditor editor=tblZeiten.getCellEditor(); if (editor!=null) editor.cancelCellEditing(); final int row=selectedRow.get(); final int col=selectedCol.get(); if (row<0||col<0) return; SwingUtilities.invokeLater(new Runnable() { @Override public void run() { // http://book.javanb.com/the-java-developers-almanac-1-4/egs/javax.swing.table/Sel.html tblZeiten.changeSelection(row,col, false, false); } }); } }); 
0
source

Source: https://habr.com/ru/post/1369058/


All Articles