Here you can find the source of selectRows(JTable table, String[] values, int column)
Parameter | Description |
---|---|
table | the table |
values | a list of values |
column | the column of interest |
public static void selectRows(JTable table, String[] values, int column)
//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); } } } } } }