Java JTable Data selectRows(JTable table, String[] values, int column)

Here you can find the source of selectRows(JTable table, String[] values, int column)

Description

Selects rows which have one of the values in the input array in the specified column.

License

Open Source License

Parameter

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

Declaration

public static void selectRows(JTable table, String[] values, int column) 

Method Source Code

//package com.java2s;
/*/*  ww w  .  j  av  a 2s. co 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.HashMap;

import java.util.Map;

import javax.swing.JTable;

public class Main {
    /**
     * Selects rows which have one of the values in the input array in the
     * specified column.  If there are multiple rows with the same value, only
     * the last row in the table with that value is selected.
     *
     * @param table   the table
     * @param values  a list of values
     * @param column  the column of interest
     */
    public static void selectRows(JTable table, String[] values, int column) {

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

                // For each row, create a mapping between the value in the specified 
                // column and the row's index.  Note that if there are multiple 
                // rows that have the same value, only the last row will be mapped.
                Map valueRowMapping = new HashMap();
                int rowCount = table.getRowCount();
                for (int row = 0; row < rowCount; row++) {
                    valueRowMapping.put(table.getValueAt(row, column), new Integer(row));
                }

                // Now look up the indices of the rows mapped to by the values in the input
                // array and select them.
                table.clearSelection();
                for (int i = 0; i < values.length; i++) {
                    if ((values[i] != null) && (valueRowMapping.containsKey(values[i]))) {
                        int row = ((Integer) valueRowMapping.get(values[i])).intValue();
                        table.addRowSelectionInterval(row, row);
                    }
                }
            }
        }
    }
}

Related

  1. getSelectValue(JTable table, String columnName)
  2. getStringValueAt(JTable table, int row, int columnIndex)
  3. getValueAt(JTable table, int row, String columnTitle)
  4. getValueBySelectedRow(JTable table, int rows[], int col)
  5. retrieveSelectedValuesFromTable(JTable table, int column)
  6. selectValueInTable(JTable table, String value, int column)
  7. setModelValueAt(JTable table, Object value, int row, String columnTitle)
  8. setValueAt(JTable table, Object value, int row, String columnTitle)
  9. tableColumnValueAlign(JTable table, int column, int align)