Java JTable Model compactDefaultTableModel(DefaultTableModel defaultTableModel)

Here you can find the source of compactDefaultTableModel(DefaultTableModel defaultTableModel)

Description

compact Default Table Model

License

Open Source License

Declaration

public static int compactDefaultTableModel(DefaultTableModel defaultTableModel) 

Method Source Code

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

import javax.swing.table.DefaultTableModel;

public class Main {
    public static int compactDefaultTableModel(DefaultTableModel defaultTableModel) {
        int destIndex = 0;
        int lastSourceIndexNonEmpty = -1; // Indicate none found
        int count = 0;
        for (int sourceIndex = 0; sourceIndex < defaultTableModel.getRowCount(); sourceIndex++) {
            Object object = defaultTableModel.getValueAt(sourceIndex, 0);
            if (object != null) {
                String entry = object.toString().trim();
                entry = entry.trim();/*from w w  w.j ava  2 s .  com*/
                if (!entry.isEmpty()) {
                    lastSourceIndexNonEmpty = sourceIndex;
                    defaultTableModel.setValueAt(entry, destIndex++, 0);
                    count++;
                }
            }
        }
        if (-1 != lastSourceIndexNonEmpty) { // Something in table, MAY need to clear out rows at end:
            while (destIndex <= lastSourceIndexNonEmpty) {
                defaultTableModel.setValueAt("", destIndex++, 0);
            }
        }
        return count; // Return number of entries encountered.
    }
}

Related

  1. convertModelIndexToView(JTable table, int modelIndex)
  2. convertSelectionToModel(JTable table)
  3. convertTableToList(DefaultTableModel table)
  4. dumpToText(TableModel m, String filename)