我目前正在使用这两种方法来设置某些文本字段,这些字段等于JTable中当前选择的行。当我过滤该表时,我遇到了一个问题。过滤数据时,文本字段不会填充正确的数据。如果表中没有过滤器,则字段将填充该位置中的行数据。任何人都有任何建议或其他方式我可以将表字段与文本字段绑定?
private void tblEmployeeMouseClicked(java.awt.event.MouseEvent evt) {
try {
int row = tblEmployee.getSelectedRow();
String Table_click = (tblEmployee.getModel().getValueAt(row, 0).toString());
String sql = "select * from Employee where EmployeeID = " + Table_click + " ";
newDatabase.populateEmployee(sql);
}
catch(Exception e) {
JOptionPane.showMessageDialog(null, e);
e.printStackTrace();
}
}
public void populateEmployee(String sql) throws Exception{
Constructor("newDatabase");
pst = connection.prepareStatement(sql);
ResultSet rs = pst.executeQuery();
if(rs.next()) {
String add1 = rs.getString(1);
NewJFrame.txtEmpID.setText(add1);
String add2 = rs.getString(2);
NewJFrame.txtEmpFN.setText(add2);
String add3 = rs.getString(3);
NewJFrame.txtEmpLN.setText(add3);
String add4 = rs.getString(4);
NewJFrame.txtEmpMI.setText(add4);
String add5 = rs.getString(5);
NewJFrame.txtEmpAddress.setText(add5);
String add6 = rs.getString(6);
NewJFrame.txtEmpState.setText(add6);
String add7 = rs.getString(7);
NewJFrame.txtEmpZIP.setText(add7);
String add8 = rs.getString(8);
NewJFrame.txtEmpDOB.setText(add8);
String add9 = rs.getString(9);
NewJFrame.txtEmpHire.setText(add9);
String add10 = rs.getString(10);
NewJFrame.txtEmpTerm.setText(add10);
String add11 = rs.getString(11);
NewJFrame.txtEmpLic.setText(add11);
String add12 = rs.getString(12);
NewJFrame.txtEmpActive.setText(add12);
String add13 = rs.getString(13);
NewJFrame.txtEmpMan.setText(add13);
String add14 = rs.getString(14);
NewJFrame.txtEmpMod.setText(add14);
}
connection.close();
}