Java JTable Model convertTableToList(DefaultTableModel table)

Here you can find the source of convertTableToList(DefaultTableModel table)

Description

Returns a list as a table with the first line representing the name of the columns.

License

LGPL

Parameter

Parameter Description
table a parameter

Return

a synchronized list with table data.

Declaration

private static List<String> convertTableToList(DefaultTableModel table) 

Method Source Code

//package com.java2s;
//License from project: LGPL 

import java.util.List;

import java.util.Vector;

import javax.swing.table.DefaultTableModel;

public class Main {
    /**//from   w  w w .  j a v  a  2s.c  o  m
     * Returns a list as a table with the first line representing the name of
     * the columns. The other rows represent table data.
     * 
     * @param table
     * @return a synchronized list with table data.
     */
    private static List<String> convertTableToList(DefaultTableModel table) {
        List<String> schedule = new Vector<String>();
        StringBuilder builder = new StringBuilder();

        int numCol = table.getColumnCount();
        int numRow = table.getRowCount();
        // Add column names
        for (int i = 0; i < numCol; i++) {
            builder.append(table.getColumnName(i)).append(" ");
        }
        schedule.add(builder.toString());
        // build data
        for (int i = 0; i < numRow; i++) {
            builder = new StringBuilder();
            for (int j = 0; j < numCol; j++) {
                String s = (String) table.getValueAt(i, j);
                if (s == null) {
                    s = "#";
                }
                s = s.trim();
                builder.append(s).append(" ");
            }
            schedule.add(builder.toString());
        }
        return schedule;
    }
}

Related

  1. compactDefaultTableModel(DefaultTableModel defaultTableModel)
  2. convertModelIndexToView(JTable table, int modelIndex)
  3. convertSelectionToModel(JTable table)
  4. dumpToText(TableModel m, String filename)
  5. fireTableDataChangedAndKeepSelection(final AbstractTableModel tableModel, final JTable table)
  6. getDefaultTableModel()
  7. getSelectedModelIndex(JTable table)