I use this Java code to get name
from a JTable
with selecting a row.
String name=table.getModel().getValueAt(table.getSelectedRow(), 0).toString();
I want to know how to do the same but with selecting two rows from table
at the same time and get the values for the variables String name
and String name1
. UPDATE: solution
table.getSelectionModel().setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
int[] rows = table.getSelectedRows();
String name1=table.getModel().getValueAt(rows[1] , 0).toString();
String name2=table.getModel().getValueAt(rows[0] , 0).toString();```
You can use table.getSelectedRows();
to get multiple selected rows.
int[] rows = table.getSelectedRows();
String name = table.getModel().getValueAt(rows[0], 0).toString();
String name1 = table.getModel().getValueAt(rows[1], 0).toString();
To limit the amount of rows you can select you should make a custom mouselistener
that selects and deselect rows.
table.setEnabled(false);
table.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
Point p = e.getPoint();
int row = table.rowAtPoint(p);
ListSelectionModel i = table.getSelectionModel();
// Toggle clicked row and check if there is only two rows selected
if (i.isSelectedIndex(row)) i.removeSelectionInterval(row, row);
else if (table.getSelectedRows().length < 2) i.addSelectionInterval(row, row);
}
});
For me use checkbox instead to select multiple rows. Or make a selection mode option like dropdownbox and then get the data of a selected rows like ID and then store it in array. Use those id that you stored in array and query it.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.