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
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});
}
the methode that checks selected rows (i'm sure it gets excecuted)
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();
}
}
output of this methode when i select row 2 to 5
aantal rijen: 8
selected rows: 0
first selected rows: -1