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

intcompactDefaultTableModel(DefaultTableModel defaultTableModel)
compact Default Table Model
int destIndex = 0;
int lastSourceIndexNonEmpty = -1; 
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();
...
intconvertModelIndexToView(JTable table, int modelIndex)
Converts an index as it appears in a model to one in the view
for (int col = 0; col < table.getColumnCount(); col++) {
    if (table.getColumnModel().getColumn(col).getModelIndex() == modelIndex) {
        return col;
return -1;
int[]convertSelectionToModel(JTable table)
Converts the selected view indexes to model indexes.
int[] selected = table.getSelectedRows();
for (int i = 0; i < selected.length; i++) {
    selected[i] = table.convertRowIndexToModel(selected[i]);
return selected;
ListconvertTableToList(DefaultTableModel table)
Returns a list as a table with the first line representing the name of the columns.
List<String> schedule = new Vector<String>();
StringBuilder builder = new StringBuilder();
int numCol = table.getColumnCount();
int numRow = table.getRowCount();
for (int i = 0; i < numCol; i++) {
    builder.append(table.getColumnName(i)).append(" ");
schedule.add(builder.toString());
...
voiddumpToText(TableModel m, String filename)
dump To Text
try {
    PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(filename)));
    int nc = m.getColumnCount();
    int nr = m.getRowCount();
    StringBuffer buf = new StringBuffer(nc * 30);
    for (int i = 0; i < nc; i++) {
        buf.append("\t").append(m.getColumnName(i));
    writer.println(buf.toString());
    for (int i = 0; i < nr; i++) {
        for (int j = 0; j < nc; j++) {
            writer.print("\t");
            writer.print(m.getValueAt(i, j).toString());
        writer.println();
    writer.close();
} catch (IOException ioe) {
    System.err.println("Error " + ioe.getMessage() + "dumping table to text");
voidfireTableDataChangedAndKeepSelection(final AbstractTableModel tableModel, final JTable table)
DOCUMENT ME!
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() {
...
DefaultTableModelgetDefaultTableModel()
get Default Table Model
return new DefaultTableModel() {
    @Override
    public boolean isCellEditable(int row, int column) {
        return false;
};
intgetSelectedModelIndex(JTable table)
get Selected Model Index
int selectedRow = table.getSelectedRow();
if (selectedRow < 0) {
    return -1;
return rowToModelIndex(table, selectedRow);
LinkedHashMapgetTableContent(TableModel table)
get Table Content
int rowCount = table.getRowCount();
LinkedHashMap<String, String> result = new LinkedHashMap<>();
for (int row = 0; row < rowCount; row++) {
    String key = (String) table.getValueAt(row, 0);
    if (TextUtils.isEmpty(key))
        continue;
    String value = (String) table.getValueAt(row, 1);
    result.put(key, value);
...
voidinsertTableModelListener(TableModel model, TableModelListener l, int index)
insert Table Model Listener
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);