Java JTable Data findFirstRow(TableModel model, int col, String value)

Here you can find the source of findFirstRow(TableModel model, int col, String value)

Description

Finds the first row of a TableModel that contains a specific String value in a given column.

License

Open Source License

Parameter

Parameter Description
model The TableModel to search through
col The column index to search under.
value The value of the cell to look for.

Return

The index of the matched row or -1 if not found.

Declaration

public static int findFirstRow(TableModel model, int col, String value) 

Method Source Code

//package com.java2s;
/*//from   ww w . j a va2 s.  c  om
 * Copyright (c) 2008, SQL Power Group Inc.
 *
 * This file is part of Power*Architect.
 *
 * Power*Architect 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.
 *
 * Power*Architect 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, see <http://www.gnu.org/licenses/>. 
 */

import javax.swing.table.TableModel;

public class Main {
    /**
     * Finds the first row of a {@link TableModel} that contains a specific
     * {@link String} value in a given column. Note that all leading and trailing
     * spaces are trimmed when matching.
     * 
     * @param model
     *            The {@link TableModel} to search through
     * @param col
     *            The column index to search under.
     * @param value
     *            The value of the cell to look for.
     * @return The index of the matched row or -1 if not found.
     */
    public static int findFirstRow(TableModel model, int col, String value) {
        String trimmedValue = value.trim();
        for (int i = 0; i < model.getRowCount(); i++) {
            if (((String) model.getValueAt(i, col)).trim().equals(
                    trimmedValue)) {
                return i;
            }
        }
        return -1;
    }
}

Related

  1. addMissingRows(DefaultTableModel model, String[] values, int column)
  2. columnContains(TableModel table, int colIdx, T... values)
  3. getRenderedComponent(JTable table, Object value, int row, int column)
  4. getRowByValue(TableModel model, int columnIndex, Object value)
  5. getRowIndex(JTable table, int column, String value)
  6. getSelectedValues(JTable table, int column)