List of utility methods to do JTable Row Selection
void | fireSelectRow(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); }); |
int | getSelectedModelRow(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; ... |
void | moveRowSelection(JTable pJTable, int pRow) move Row Selection Rectangle rect = pJTable.getCellRect(pRow, 0, true); pJTable.scrollRectToVisible(rect); pJTable.clearSelection(); pJTable.setRowSelectionInterval(pRow, pRow); |
void | removeSelectedRowFromTable(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++) { ... |
void | selectClickedRow(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); |
void | selectLastTableRow(JTable table) Select the last row in a table. table.clearSelection();
int index = table.getRowCount() - 1;
index = table.convertRowIndexToView(index);
table.addRowSelectionInterval(index, index);
|
void | setTableSelectedRows(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; |