I added an enumerator with a mouse click on my jtable, when I double-clicked a line, a window will open accordingly.
jTable.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent e) {
double amount = Double.parseDouble(jTable.getValueAt(getSelectedRow(), 4).toString());
String remarks = jTable.getValueAt(getSelectedRow(), 3).toString();
String transactionID = jTable.getValueAt(getSelectedRow(), 1).toString();
new EditFrame(...)
}
});
I used this code to retrieve the row selected next.
public int getSelectedRow() {
jTable.getSelectionModel().addListSelectionListener(
new ListSelectionListener() {
public void valueChanged(ListSelectionEvent event) {
int viewRow = jTable.getSelectedRow();
selectedRow = viewRow;
System.out.println(viewRow);
}
});
return selectedRow;
}
In my case, I realized when I first clicked the second row, I get null for selectedRow, only when I select the first row and then the second row, then I can get the correct data. And if I delete the mouse listener, then the problem will be solved. Is it because I'm doing something wrong while listening to mouse clicks?
source
share