Java JTable Model fireTableDataChangedAndKeepSelection(final AbstractTableModel tableModel, final JTable table)

Here you can find the source of fireTableDataChangedAndKeepSelection(final AbstractTableModel tableModel, final JTable table)

Description

DOCUMENT ME!

License

Open Source License

Parameter

Parameter Description
tableModel DOCUMENT ME!
table DOCUMENT ME!

Declaration

public static void fireTableDataChangedAndKeepSelection(final AbstractTableModel tableModel,
        final JTable table) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.AbstractTableModel;

public class Main {
    /**/*ww w.j  ava 2s.c o  m*/
     * DOCUMENT ME!
     *
     * @param  tableModel  DOCUMENT ME!
     * @param  table       DOCUMENT ME!
     */
    public static void fireTableDataChangedAndKeepSelection(final AbstractTableModel tableModel,
            final JTable table) {
        final int selection_view = table.getSelectedRow();

        int selection_model_tmp = -1;
        if (selection_view > -1) {
            selection_model_tmp = table.convertRowIndexToModel(selection_view);
        }
        final int selection_model = selection_model_tmp;

        tableModel.fireTableDataChanged();

        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                if ((selection_view == -1) || (selection_view >= table.getRowCount())) {
                    table.clearSelection();
                } else {
                    final int selection_view_tmp = table.convertRowIndexToView(selection_model);
                    table.setRowSelectionInterval(selection_view_tmp, selection_view_tmp);
                    table.scrollRectToVisible(table.getCellRect(selection_view_tmp, 0, true));
                }
            }
        });
    }
}

Related

  1. compactDefaultTableModel(DefaultTableModel defaultTableModel)
  2. convertModelIndexToView(JTable table, int modelIndex)
  3. convertSelectionToModel(JTable table)
  4. convertTableToList(DefaultTableModel table)
  5. dumpToText(TableModel m, String filename)
  6. getDefaultTableModel()
  7. getSelectedModelIndex(JTable table)
  8. getTableContent(TableModel table)
  9. insertTableModelListener( TableModel model, TableModelListener l, int index)