Java Utililty Methods JTable Model

List of utility methods to do JTable Model

Description

The list of methods to do JTable Model are organized into topic(s).

Method

voidinsertTableModelListener(TableModel model, TableModelListener l, int index)
Inserts the table model listener at the particular index in the listeners' chain.
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);
if (index < 0 || index > listeners.length - 1) {
    model.addTableModelListener(l);
DefaultTableModelmakeDefaultTableModel(String[] titleA, final Class[] typesA, final boolean[] canEditA)
Make DefaultTableModel by overriding getColumnClass and isCellEditable of DefaultTableModel.
return new DefaultTableModel(new Object[][] {}, titleA) {
    Class<?>[] types = typesA;
    boolean[] canEdit = canEditA;
    @Override
    public Class<?> getColumnClass(int columnIndex) {
        return types[columnIndex];
    @Override
...
intmodelIndexToDisplayIndex(JTable table, int modelIndex)
model Index To Display Index
if (modelIndex >= 0) {
    RowSorter<?> rowSorter = table.getRowSorter();
    return rowSorter != null ? rowSorter.convertRowIndexToView(modelIndex) : modelIndex;
return -1;
voidsaveTModelToCSV(String fileName, JTable table)
Saves information from a table interface element to a file in CSV format.
if (!fileName.endsWith(".csv"))
    fileName += ".csv";
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), ENCODING));
TableModel model = table.getModel();
for (int i = 0; i < table.getRowCount(); i++) {
    int index = table.convertRowIndexToModel(i);
    for (int j = 0; j < table.getColumnCount(); j++) {
        if (model.getColumnClass(j).equals(String.class))
...
voidsetDataVector(DefaultTableModel model, Vector dataVector)
Removes all existing rows in the table model and adds the rows found in the input data vector.
if (model != null) {
    if (dataVector == null) {
        dataVector = new Vector();
    } else {
        dataVector = (Vector) dataVector.clone();
    for (int row = model.getRowCount() - 1; row >= 0; row--) {
        model.removeRow(row);
...
Object[][]tableModelToArray(JTable table)
table Model To Array
return tableModelToArray(table, false);
ArrayListTableModelToList(JTable mode)
Table Model To List
ArrayList<String[]> list = new ArrayList<String[]>();
DefaultTableModel model = (DefaultTableModel) mode.getModel();
for (int i = 0; i < model.getRowCount(); i++) {
    String[] str = null;
    for (int j = 0; j < model.getColumnCount(); j++) {
        str = new String[model.getColumnCount()];
        str[j] = model.getValueAt(i, j).toString();
    list.add(str);
return list;
StringtoCsv(TableModel model)
Convert the given table model to comma separated string
return toCsv(model, false);
DefaultTableModeltoTableModel(HashMap data)
to Table Model
Object[] array = data.keySet().toArray();
Object[][] table = new Object[array.length][1];
for (int i = 0; i < array.length; i++) {
    table[i][0] = array[i];
return new DefaultTableModel(table, new Object[] { "Devices" });