Я могу получить искомый элемент в jTable
. Но когда я щелкнул по этой строке, в jTextField
отображается не та строка.
Все в порядке. Но когда я выбираю первый элемент из отсортированной строки, он отображает первый элемент, в котором я сохранил базу данных.
Вот мой код для отображения деталей, щелчка мышью по таблице и поиска.
public ArrayList<User> userList() {
ArrayList<User> usersList = new ArrayList<>();
try {
//connection
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/student","root","");
String query1 = "SELECT * FROM `stu_details`";
Statement st = con.createStatement();
ResultSet rs = st.executeQuery(query1);
User user;
while (rs.next()) {
user = new User(rs.getString("id"),rs.getString("Full_name"),rs.getString("course"),rs.getString("address"),rs.getString("gender"),rs.getString("gur_num"),rs.getString("dob"),rs.getString("ph_num"),rs.getString("batch"));
usersList.add(user);
}
} catch (Exception e){
JOptionPane.showMessageDialog(null,e);
}
return usersList;
}
public void show_user() {
ArrayList<User> list = userList();
DefaultTableModel model = (DefaultTableModel) table.getModel();
Object[] row = new Object[20];
for (int i = 0; i < list.size(); i++) {
row[1] = list.get(i).getid();
row[0] = list.get(i).getfullname();
row[2] = list.get(i).getcourse();
row[3] = list.get(i).getaddress();
row[4] = list.get(i).getgender();
row[5] = list.get(i).getgur_num();
row[6] = list.get(i).getdob();
row[7] = list.get(i).getph_num();
row[8] = list.get(i).getbatch();
model.addRow(row);
}
}
private void tableMouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
int i = table.getSelectedRow();
TableModel model = table.getModel();
id.setText(model.getValueAt(i,0).toString());
name.setText(model.getValueAt(i,1).toString());
String subject = model.getValueAt(i, 2).toString();
switch (subject) {
case "Spoken English WeedDay":
course.setSelectedIndex(0);
break;
case "Spoken English WeedEnd":
course.setSelectedIndex(1);
break;
case "IELTS":
course.setSelectedIndex(2);
break;
case "BIT":
course.setSelectedIndex(3);
break;
case "HND":
course.setSelectedIndex(4);
break;
}
address.setText(model.getValueAt(i,3).toString());
String sex = model.getValueAt(i,4).toString();
if (sex.equals("Male")) {
male.setSelected(true);
}
else {
female.setSelected(true);
}
gur_num.setText(model.getValueAt(i,5).toString());
try {
Date date = new SimpleDateFormat("yyyy-MM-dd").parse((String)model.getValueAt(i,6).toString());
dob.setDate(date);
} catch (ParseException ex) {
Logger.getLogger(Copy.class.getName()).log(Level.SEVERE, null, ex);
}
ph_num.setText(model.getValueAt(i,7).toString());
String batch1 = model.getValueAt(i, 8).toString();
switch (batch1) {
case "WD_01":
course.setSelectedIndex(0);
break;
case "WD_02":
course.setSelectedIndex(1);
break;
case "WD_03":
course.setSelectedIndex(2);
break;
case "WD_04":
course.setSelectedIndex(3);
break;
case "WE_01":
course.setSelectedIndex(4);
break;
case "WE_02":
course.setSelectedIndex(5);
break;
case "WE_03":
course.setSelectedIndex(6);
break;
case "WE_04":
course.setSelectedIndex(7);
break;
case "IE_01":
course.setSelectedIndex(8);
break;
case "IE_02":
course.setSelectedIndex(9);
break;
case "IE_03":
course.setSelectedIndex(10);
break;
case "IE_04":
course.setSelectedIndex(11);
break;
case "BI_01":
course.setSelectedIndex(12);
break;
case "BI_02":
course.setSelectedIndex(13);
break;
case "HD_01":
course.setSelectedIndex(14);
break;
case "HD_02":
course.setSelectedIndex(15);
break;
}
}
private void searchKeyReleased(java.awt.event.KeyEvent evt) {
DefaultTableModel table1 = (DefaultTableModel)table.getModel();
String search1 = search.getText().toString();
TableRowSorter<DefaultTableModel> tr = new TableRowSorter<DefaultTableModel>(table1);
table.setRowSorter(tr);
tr.setRowFilter(RowFilter.regexFilter(search1));
}