Here you can find the source of getSelectedModelRow(JTable table)
Parameter | Description |
---|---|
table | the table for which the selected model row is needed |
public static int getSelectedModelRow(JTable table)
//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; } }