我有这个动作ActionFindPeople
,我想在 db 中搜索插入 2 个不同文本字段的姓名和姓氏的人。
public class ActionFindPeople extends AbstractAction{
private Control control;
public ActionFindPeople (Control control) {
this.control = control;
this.putValue(Action.NAME, "FIND");
this.putValue(Action.SHORT_DESCRIPTION, "Find People");
this.putValue(Action.MNEMONIC_KEY, new Integer(KeyEvent.VK_F));
this.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke("CTRL F"));
}
public void actionPerformed(ActionEvent e) {
Vist vist = (Vist) this.control.getVist();
PanelInitial pi = (PanelInitial ) vist.getUnderVist(Costant.PANEL_INITIAL);
PannelResult pr = (PannelResult) vist.getUnderVist(Costant.PANEL_RESULT);
Model model = (Model) this.control.getModel();
String name = pi.getTextFieldName().getText();
String surname = pi.getTextFieldSurname().getText();
Db db= (Db) control.getModel().getBean(Costant.DB);
Person personToFind = findPerson(name, surname, db);
if(personToFind != null) {
model.putBean(Costant.PERSON_FOUND, personToFind );
prp.changeComponent((Person) this.control.getModel().getBean(Costant.PERSON_FOUND));
vist.changePanel();
} else {
vist.windowError("Insesistent people for " + name+ " " + surname);
}
}
public Person findPerson(String name, String surname, Db db) {
List<Person> listPerson = db.getListPerson();
for (Person p : listPerson){
if(p.getName().equalsIgnoreCase(name) && p.getSurname().equalsIgnoreCase(surname)){
return p;
}
}
return null;
}
}
我想尝试将姓名和姓氏相等的人放在 jTable 中,但是当我这样做时,如果有人存在,它会将我转回数据库中的所有人!我怎么解决这个问题???这也是刷新表格模型的代码。
public void refreshModel() {
Model model = (Model) this.vist.getModel();
Person person = (Person) model.getBean(Costanti.PERSON);
Db a = (Db) model.getBean(Costanti.DB);
ModelTable mt = new ModelTable (a);
this.jTable.setModel(mt);
this.jTable.setSelectionMode(SINGLE_SELECTION);
this.jScrollPane1.setViewportView(this.jTable);
}