Getting the Selected Cells in a JTable Component
The method for determining the selected cells depends on whether
column, row, or cell selection is enabled.
JTable table = new JTable();
if (table.getColumnSelectionAllowed()
&& !table.getRowSelectionAllowed()) {
// Column selection is enabled
// Get the indices of the selected columns
int[] vColIndices = table.getSelectedColumns();
} else if (!table.getColumnSelectionAllowed()
&& table.getRowSelectionAllowed()) {
// Row selection is enabled
// Get the indices of the selected rows
int[] rowIndices = table.getSelectedRows();
} else if (table.getCellSelectionEnabled()) {
// Individual cell selection is enabled
// In SINGLE_SELECTION mode, the selected cell can be retrieved using
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
int rowIndex = table.getSelectedRow();
int colIndex = table.getSelectedColumn();
// In the other modes, the set of selected cells can be retrieved using
table.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
// Get the min and max ranges of selected cells
int rowIndexStart = table.getSelectedRow();
int rowIndexEnd = table.getSelectionModel().getMaxSelectionIndex();
int colIndexStart = table.getSelectedColumn();
int colIndexEnd = table.getColumnModel().getSelectionModel()
.getMaxSelectionIndex();
// Check each cell in the range
for (int r=rowIndexStart; r<=rowIndexEnd; r++) {
for (int c=colIndexStart; c<=colIndexEnd; c++) {
if (table.isCellSelected(r, c)) {
// cell is selected
}
}
}
}
i made a jtable in java with 2 columns
when i select some rows and click a button it should get the selected rows
but this array comes back empty
here are some sniplets
[CODE] JScrollPane scroller = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scroller.setBorder(null);
this.add(scroller,BorderLayout.CENTER);
customers = new JTable();
scroller.setViewportView(customers);
customers.setAutoCreateRowSorter(true);
customers.setModel(new DefaultTableModel(new Object[][]{}, new String[]{"Naam", "Nummer"}) {
@Override
public Class getColumnClass(int columnIndex) {
return String.class;
}
@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
if (columnIndex == 0) {
return true;
}
return false;
}
});
customers.setColumnSelectionAllowed(false);
customers.getTableHeader().setReorderingAllowed(false);
customers.getColumnModel().getSelectionModel().setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
for (String user : users) {
((DefaultTableModel)customers.getModel()).addRow(new Object[]{"", user});
}[/CODE]
the methode that checks selected rows (i'm sure it gets excecuted)
[CODE] public void analyseSelection() {
System.out.println("aantal rijen: " + customers.getRowCount());
int[] selected = customers.getSelectedRows();
System.out.println("selected rows: " + selected.length);
System.out.println("first selected rows: " + customers.getSelectedRow());
names = new String[selected.length];
numbers = new String[selected.length];
for (int i : selected) {
names[i] = customers.getValueAt(i, 0).toString();
numbers[i] = customers.getValueAt(i, 1).toString();
}
}[/CODE]
output of this methode when i select row 2 to 5
[CODE]aantal rijen: 8
selected rows: 0
first selected rows: -1[/CODE]