我有一个名为jListCustSearchResults的jList对象,它包含许多CustomerEntity对象,并将它们显示为用户选择客户的列表。
下面的第一个方法是JButton的actionperformed方法,它在单击时触发此JList的更新序列。它调用名为fillCustomerList的另一个函数,用从数据库中检索的新Customers重新填充JList对象。
这里的问题是所提到的jList对象没有在gui中更新。相反,它完全是空的。作为替代解决方案,我将方法refillCustomerList放入其doBackground方法中的SwingWorker对象中,以便在EDT中不会发生更新。但是,jLIst仍未使用GUI上的新内容进行更新。为什么你认为它没有更新?
在此消息的底部,我放置了我的实现的SwingWorker变体。 jList仍未在gui中更新(我也调用了repaint())。
private void jTextFieldCustomerSearchWordActionPerformed(ActionEvent evt) {
int filterType = jComboBoxSearchType.getSelectedIndex() + 1;
String filterWord = jTextFieldCustomerSearchWord.getText();
try {
controller.repopulateCustomerListByFilterCriteria(filterType, filterWord);
} catch (ApplicationException ex) {
Helper.processExceptionLog(ex, true);
}
refillCustomerList();
}
private void refillCustomerList() {
if (jListCustSearchResults.getModel().getSize() != 0) {
jListCustSearchResults.removeAll();
}
jListCustSearchResults.setModel(new javax.swing.AbstractListModel() {
List<CustomerEntity> customerList = controller.getCustomerList();
@Override
public int getSize() {
return customerList.size();
}
@Override
public Object getElementAt(int i) {
return customerList.get(i);
}
});
jListCustSearchResults.setSelectedIndex(0);
}
==========================
private void jTextFieldCustomerSearchWordActionPerformed(ActionEvent evt) {
SwingWorker worker = new SwingWorker<Void, Void>() {
@Override
public void done() {
repaint();
}
@Override
protected Void doInBackground() throws Exception {
int filterType = jComboBoxSearchType.getSelectedIndex() + 1;
String filterWord = jTextFieldCustomerSearchWord.getText();
try {
controller.repopulateCustomerListByFilterCriteria(filterType, filterWord);
} catch (ApplicationException ex) {
Helper.processExceptionLog(ex, true);
}
refillCustomerList();
return null;
}
};
worker.execute();
}