Java JTable Model setDataVector(DefaultTableModel model, Vector dataVector)

Here you can find the source of setDataVector(DefaultTableModel model, Vector dataVector)

Description

Removes all existing rows in the table model and adds the rows found in the input data vector.

License

Open Source License

Parameter

Parameter Description
model the table model
dataVector the new table rows

Declaration

public static void setDataVector(DefaultTableModel model, Vector dataVector) 

Method Source Code

//package com.java2s;
/*/*  ww w . j  av  a 2s  .c o m*/
 TSAFE Prototype: A decision support tool for air traffic controllers
 Copyright (C) 2003  Gregory D. Dennis
    
 This program 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 2
 of the License, or (at your option) any later version.
    
 This program 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 this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

import java.util.Vector;

import javax.swing.table.DefaultTableModel;

public class Main {
    /**
     * Removes all existing rows in the table model and adds the rows found
     * in the input data vector.  Doing this allows all the table settings
     * (i.e. column sizes, policies, etc.) to remain intact.
     *
     * @param model      the table model
     * @param dataVector the new table rows
     */
    public static void setDataVector(DefaultTableModel model, Vector dataVector) {

        // Make sure the input parameters are valid.
        if (model != null) {

            // Clone the input vector.  This handles the case where the input data vector
            // originated from a call to "model.getDataVector()".  That method exposes
            // the rep of the table model (i.e. returns the actual data vector).  If we
            // don't clone the vector, then removing all rows from the model in the next
            // step will cause the input data vector to become empty!
            if (dataVector == null) {
                dataVector = new Vector();
            } else {
                dataVector = (Vector) dataVector.clone();
            }

            // Remove all existing rows from the table model.
            for (int row = model.getRowCount() - 1; row >= 0; row--) {
                model.removeRow(row);
            }

            // Add new rows.
            for (int i = 0; i < dataVector.size(); i++) {
                Vector newRow = (Vector) dataVector.get(i);
                model.addRow(newRow);
            }
        }
    }
}

Related

  1. insertTableModelListener( TableModel model, TableModelListener l, int index)
  2. insertTableModelListener(TableModel model, TableModelListener l, int index)
  3. makeDefaultTableModel(String[] titleA, final Class[] typesA, final boolean[] canEditA)
  4. modelIndexToDisplayIndex(JTable table, int modelIndex)
  5. saveTModelToCSV(String fileName, JTable table)
  6. tableModelToArray(JTable table)
  7. TableModelToList(JTable mode)
  8. toCsv(TableModel model)
  9. toTableModel(HashMap data)