Java JTable Data addMissingRows(DefaultTableModel model, String[] values, int column)

Here you can find the source of addMissingRows(DefaultTableModel model, String[] values, int column)

Description

For each value in the input array that does not appear in the specified column of at least one of the table model's rows, a new row is added with that value in the specified column and the empty string in the remaining columns.

License

Open Source License

Parameter

Parameter Description
model the table model
values a list of values
column the column of interest

Return

the number of new rows actually inserted

Declaration

public static int addMissingRows(DefaultTableModel model, String[] values, int column) 

Method Source Code

//package com.java2s;
/*//from  w  w  w.jav a  2s.com
 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.HashSet;

import java.util.Set;
import java.util.Vector;

import javax.swing.table.DefaultTableModel;

public class Main {
    /**
     * For each value in the input array that does not appear in the specified column
     * of at least one of the table model's rows, a new row is added with that value
     * in the specified column and the empty string in the remaining columns.  Note
     * that null values in the input array are ignored.
     *
     * @param model   the table model
     * @param values  a list of values
     * @param column  the column of interest
     * @return        the number of new rows actually inserted
     */
    public static int addMissingRows(DefaultTableModel model, String[] values, int column) {
        int numInserted = 0;

        // Make sure the input parameters are valid.
        if ((values != null) && (model != null)) {
            int columnCount = model.getColumnCount();
            if ((column >= 0) && (column < columnCount)) {

                // Make a list of all unique values in the specified column
                // for all rows in the table model.
                Set existingValues = new HashSet();
                int rowCount = model.getRowCount();
                for (int row = 0; row < rowCount; row++) {
                    existingValues.add(model.getValueAt(row, column));
                }

                // Now, if a value in the input array does not already exist in the specified column
                // of some row in the table model, go ahead and add a new row with that value.
                for (int i = 0; i < values.length; i++) {
                    if ((values[i] != null) && (!existingValues.contains(values[i]))) {
                        Vector newRow = new Vector(columnCount);
                        for (int j = 0; j < columnCount; j++) {
                            if (j == column) {
                                newRow.add(values[i]);
                            } else {
                                newRow.add("");
                            }
                        }
                        model.addRow(newRow);
                        existingValues.add(values[i]);
                        numInserted++;
                    }
                }
            }
        }

        return numInserted;
    }
}

Related

  1. columnContains(TableModel table, int colIdx, T... values)
  2. findFirstRow(TableModel model, int col, String value)
  3. getRenderedComponent(JTable table, Object value, int row, int column)
  4. getRowByValue(TableModel model, int columnIndex, Object value)