Java JTable Data getSelectedValues(JTable table, int column)

Here you can find the source of getSelectedValues(JTable table, int column)

Description

Returns the value of the specified column for each row currently selected in the table.

License

Open Source License

Parameter

Parameter Description
table the table
column the column of interest

Return

an array of Strings

Declaration

public static String[] getSelectedValues(JTable table, int column) 

Method Source Code

//package com.java2s;
/*//from w  ww .  java 2s. c om
 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 javax.swing.JTable;

public class Main {
    /**
     * Returns the value of the specified column for each row
     * currently selected in the table.
     *
     * @param table   the table
     * @param column  the column of interest
     * @return        an array of Strings
     */
    public static String[] getSelectedValues(JTable table, int column) {
        String[] selectedValues = new String[0];

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

                // Get the indices of all selected rows, then extract the
                // value at the specified column for each row.
                int[] selectedRows = table.getSelectedRows();
                selectedValues = new String[selectedRows.length];
                for (int i = 0; i < selectedRows.length; i++) {
                    selectedValues[i] = (String) table.getValueAt(selectedRows[i], column);
                }
            }
        }

        return selectedValues;
    }
}

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)
  5. getRowIndex(JTable table, int column, String value)
  6. getSelectValue(JTable table, String columnName)
  7. getStringValueAt(JTable table, int row, int columnIndex)
  8. getValueAt(JTable table, int row, String columnTitle)
  9. getValueBySelectedRow(JTable table, int rows[], int col)