I added a mouse clicked listner to my jtable, when i double click the row, will pop up an window 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 EditFr开发者_StackOverflowame(...)
}
});
This code I used to retrieve the row selected row.
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 realised when I clicked the second row in the first time, I get null for selectedRow, only when I select first row then second row, I then can get the correct data. And If I removed the mouse listener the problem then be solved. Is it because I doing something wrong at the mouse click listener?
If you just want to know what row was clicked then you don't need the selection listener. Just use:
table.rowAtPoint();
You're doing it the wrong way. Remove your current getSelectedRow()
method completely and never try to code something similar. Here is a better version:
jTable.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent e) {
int selectedRow = jTable.getSelectedRow();
double amount = Double.parseDouble(jTable.getValueAt(selectedRow, 4).toString());
String remarks = jTable.getValueAt(selectedRow, 3).toString();
String transactionID = jTable.getValueAt(selectedRow, 1).toString();
new EditFrame(...)
}
});
精彩评论