Here you can find the source of compactDefaultTableModel(DefaultTableModel defaultTableModel)
public static int compactDefaultTableModel(DefaultTableModel defaultTableModel)
//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. } }