Java JTable Model TableModelToList(JTable mode)

Here you can find the source of TableModelToList(JTable mode)

Description

Table Model To List

License

Open Source License

Declaration

public static ArrayList TableModelToList(JTable mode) 

Method Source Code

//package com.java2s;
/*// www . ja  va2  s.c o  m
 * 
This file is part of YaBS.
    
YaBS is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
    
YaBS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
    
You should have received a copy of the GNU General Public License
along with YaBS.  If not, see <http://www.gnu.org/licenses/>.
    
 * 
 */

import java.util.ArrayList;

import java.util.Iterator;

import javax.swing.JTable;

import javax.swing.table.DefaultTableModel;

public class Main {
    public static ArrayList TableModelToList(JTable mode) {
        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;
    }

    /**
     * list 1 + 2 must have same element count!
     *
     * @param list1
     * @param list2
     * @return list1.values(n) + list2.values(n)
     * @throws Exception
     */
    public static ArrayList<Double> add(ArrayList<Double> list1, ArrayList<Double> list2) throws Exception {

        Iterator it1 = list1.iterator();
        Iterator it2 = list2.iterator();

        if (list1.size() != list2.size()) {
            throw new Exception("list 1 + 2 must have same element count!");
        }
        @SuppressWarnings("unchecked")
        ArrayList<Double> list3 = new ArrayList();

        while (it1.hasNext() && it2.hasNext()) {
            Double value = (Double) it1.next() + (Double) it2.next();
            list3.add(value);
        }

        return list3;
    }
}

Related

  1. makeDefaultTableModel(String[] titleA, final Class[] typesA, final boolean[] canEditA)
  2. modelIndexToDisplayIndex(JTable table, int modelIndex)
  3. saveTModelToCSV(String fileName, JTable table)
  4. setDataVector(DefaultTableModel model, Vector dataVector)
  5. tableModelToArray(JTable table)
  6. toCsv(TableModel model)
  7. toTableModel(HashMap data)