Java JTable Row Selection getSelectedModelRow(JTable table)

Here you can find the source of getSelectedModelRow(JTable table)

Description

Transforms the selected row index in the table view to the selected index of the underlying table model (in case the results are sorted, they can be different).

License

Open Source License

Parameter

Parameter Description
table the table for which the selected model row is needed

Return

The selected model row index or -1, if the index is out of bounds or cannot be calculated

Declaration

public static int getSelectedModelRow(JTable table) 

Method Source Code

//package com.java2s;
/* /*  ww  w.  j  ava  2 s .  co m*/
 * Copyright (C) 2014 Institute for Bioinformatics and Systems Biology, University Giessen, Germany
 *
 * 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 3 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, see <http://www.gnu.org/licenses/>.
 */

import javax.swing.DefaultListSelectionModel;
import javax.swing.JTable;

public class Main {
    /**
     * Transforms the selected row index in the table view to the selected 
     * index of the underlying table model (in case the results are sorted, 
     * they can be different). If the transformation is not possible, it returns
     * -1
     * @param table the table for which the selected model row is needed
     * @return The selected model row index or -1, if the index is out of 
     * bounds or cannot be calculated
     */
    public static int getSelectedModelRow(JTable table) {
        int wantedModelIdx = -1;
        DefaultListSelectionModel model = (DefaultListSelectionModel) table.getSelectionModel();
        int selectedView = model.getLeadSelectionIndex();

        if (table.getModel().getRowCount() > selectedView && selectedView >= 0) {
            try {
                int selectedModelIdx = table.convertRowIndexToModel(selectedView);

                if (table.getModel().getRowCount() > selectedModelIdx) {
                    wantedModelIdx = selectedModelIdx;
                }
            } catch (ArrayIndexOutOfBoundsException e) {
                //do nothing, just return -1 since the transformation was not possible
            }
        }
        return wantedModelIdx;
    }
}

Related

  1. fireSelectRow(final JTable table, final int row)
  2. moveRowSelection(JTable pJTable, int pRow)
  3. removeSelectedRowFromTable(JTable table)
  4. selectClickedRow(final JTable table, final MouseEvent event)
  5. selectLastTableRow(JTable table)