Java Utililty Methods JTable Row Selection

List of utility methods to do JTable Row Selection

Description

The list of methods to do JTable Row Selection are organized into topic(s).

Method

voidfireSelectRow(final JTable table, final int row)
fire Select Row
EventQueue.invokeLater(new Runnable() {
    @Override
    public void run() {
        if ((row >= 0) && (row < table.getRowCount())) {
            table.clearSelection();
            table.setRowSelectionInterval(row, row);
});
intgetSelectedModelRow(JTable table)
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).
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;
...
voidmoveRowSelection(JTable pJTable, int pRow)
move Row Selection
Rectangle rect = pJTable.getCellRect(pRow, 0, true);
pJTable.scrollRectToVisible(rect);
pJTable.clearSelection();
pJTable.setRowSelectionInterval(pRow, pRow);
voidremoveSelectedRowFromTable(JTable table)
remove Selected Row From Table
TableModel model = table.getModel();
Object[][] data = new Object[model.getRowCount()][model.getColumnCount()];
Object[] columnNames = new Object[model.getColumnCount()];
for (int idx = 0; idx < columnNames.length; idx++) {
    columnNames[idx] = model.getColumnName(idx);
for (int idx = 0; idx < model.getRowCount(); idx++) {
    for (int i = 0; i < model.getColumnCount(); i++) {
...
voidselectClickedRow(final JTable table, final MouseEvent event)
Selects a table row depending on a mouse event.
final int row = table.rowAtPoint(event.getPoint());
if (row == -1) {
    return;
table.setRowSelectionInterval(row, row);
voidselectLastTableRow(JTable table)
Select the last row in a table.
table.clearSelection();
int index = table.getRowCount() - 1;
index = table.convertRowIndexToView(index);
table.addRowSelectionInterval(index, index);
voidsetTableSelectedRows(JTable table, int... selectedRows)
set Table Selected Rows
if (selectedRows.length == 0) {
    table.clearSelection();
} else {
    table.clearSelection();
    for (int selectedRow : selectedRows) {
        table.addRowSelectionInterval(selectedRow, selectedRow);
Object[][]tableModelToArray(JTable table, boolean onlyTheSelectedRows)
table Model To Array
DefaultTableModel model = (DefaultTableModel) table.getModel();
Object[][] data;
int[] selections = table.getSelectedRows();
if (onlyTheSelectedRows) {
    data = new Object[selections.length][model.getColumnCount()];
} else {
    data = new Object[model.getRowCount()][model.getColumnCount()];
if (!onlyTheSelectedRows) {
    for (int idx = 0; idx < model.getRowCount(); idx++) {
        for (int i = 0; i < model.getColumnCount(); i++) {
            data[idx][i] = model.getValueAt(idx, i);
} else {
    for (int idx = 0; idx < selections.length; idx++) {
        int row = selections[idx];
        for (int i = 0; i < model.getColumnCount(); i++) {
            data[idx][i] = model.getValueAt(row, i);
return data;