Java JTable Model insertTableModelListener(TableModel model, TableModelListener l, int index)

Here you can find the source of insertTableModelListener(TableModel model, TableModelListener l, int index)

Description

Inserts the table model listener at the particular index in the listeners' chain.

License

Open Source License

Parameter

Parameter Description
model the AbstractTableModel
l the TableModelListener to be inserted
index the index.

Declaration

public static void insertTableModelListener(TableModel model,
        TableModelListener l, int index) 

Method Source Code

//package com.java2s;

import javax.swing.event.TableModelListener;

import javax.swing.table.AbstractTableModel;

import javax.swing.table.TableModel;

public class Main {
    /**//from ww  w. j  a  v a2 s  .c o m
     * Inserts the table model listener at the particular index in the listeners' chain. The listeners are fired in
     * reverse order. So the listener at index 0 will be fired at last.
     *
     * @param model the AbstractTableModel
     * @param l     the TableModelListener to be inserted
     * @param index the index.
     */
    public static void insertTableModelListener(TableModel model,
            TableModelListener l, int index) {
        if (!(model instanceof AbstractTableModel)) {
            model.addTableModelListener(l);
            return;
        }
        TableModelListener[] listeners = ((AbstractTableModel) model)
                .getTableModelListeners();
        for (TableModelListener listener : listeners) {
            model.removeTableModelListener(listener);
        }
        for (int i = 0; i < listeners.length; i++) {
            TableModelListener listener = listeners[i];
            if (index == i) {
                model.addTableModelListener(l);
            }
            model.addTableModelListener(listener);
        }
        // index is too large, add to the end.
        if (index < 0 || index > listeners.length - 1) {
            model.addTableModelListener(l);
        }
    }
}

Related

  1. fireTableDataChangedAndKeepSelection(final AbstractTableModel tableModel, final JTable table)
  2. getDefaultTableModel()
  3. getSelectedModelIndex(JTable table)
  4. getTableContent(TableModel table)
  5. insertTableModelListener( TableModel model, TableModelListener l, int index)
  6. makeDefaultTableModel(String[] titleA, final Class[] typesA, final boolean[] canEditA)
  7. modelIndexToDisplayIndex(JTable table, int modelIndex)
  8. saveTModelToCSV(String fileName, JTable table)
  9. setDataVector(DefaultTableModel model, Vector dataVector)