1

How can I handle multiple rows selection in a JTable? here is the code I have been using for selecting only one row:

table1 = new JTable();
        table1.setModel(Ajmodel);
        table1.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
            public void valueChanged(ListSelectionEvent event) {
                selectedAJ_ID = (Integer) table1.getModel().getValueAt(table1.getSelectedRow(), 0);
            }
        });

Thank you in advance.

TiyebM
  • 2,684
  • 3
  • 40
  • 66

1 Answers1

5

you can allow multiple selection by jTable.setRowSelectionAllowed(true); jTable.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

and you can get the values by

if (jTable.getSelectedRows() > -1) {

                 int[] selectedrows = jTable.getSelectedRows();

                 for (int i = 0; i < selectedrows.length; i++)
                {

                     System.out.println(jTable.getValueAt(selectedrows[i], 0).toString());

                }

            }
U X
  • 121
  • 1
  • 1
  • 6