I would like to ask for help in making a JTable, enabling the line, if a line is clicked, I read it. The problem would be that if I click on a line and then on the header (because of the sort), then the last choice appears, i.e. the modal comes up, etc. as if I had clicked on a line. Is there a solution for this or what did I do wrong? Thank you very much
my code:
`private void showUsersListInTable() {
scrollPane.setVisible(true);
scrollPane.getViewport().setSize(600, 500);
userListTable.setSize(600, 500);
getDataFromDB();
reFreshTableData();
tableModel = new DefaultTableModel(tableData, columnNames);
userListTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
userListTable.setAutoCreateRowSorter(true);
userListTable.setRowSelectionAllowed(true);
userListTable.getSelectionModel().addListSelectionListener(e -> {
if (!e.getValueIsAdjusting() && userListTable.getSelectedRow() != -1) {
int a = userListTable.convertRowIndexToModel(userListTable.getSelectedRow());
String email = userListTable.getModel().getValueAt(a, 2).toString();
int i = 0;
while (!users.get(i).getEmail().equals(email)) {
i++;
}
User modifyUser = (i > users.size() || i < 0) ? new User("", "", "", "", 0, new UserRight(false, false)) : users.get(i);
createDialog(modifyUser, frame);
}
});
scrollPane.setViewportView(userListTable);
}`
my attempts:
set jtable header (wrong,IDE use red underline.), modify sort with if(userListTable.getSelectedRow() != 0)
It appears the row selection event is generated because the selected row in the
viewrow has changed when you sort a column.You can manually track to see if the selected row in the
modelhas changed:Sorting does not affect the selected row in the model.