jListCustSearchResults という名前の jList オブジェクトがあります。これは、多数の CustomerEntity オブジェクトを保持し、ユーザーが顧客を選択するためのリストとして表示します。
以下の最初のメソッドは、クリックされたときにこの JList の更新シーケンスをトリガーする JButton のアクション実行メソッドです。次に、fillCustomerList という名前の別の関数を呼び出して、データベースから取得した新しい Customers を JList オブジェクトに再入力します。
ここでの問題は、前述の jList オブジェクトが GUI で更新されないことです。代わりに、それは完全に空です。別の解決策として、refillCustomerList メソッドを SwingWorker オブジェクトの doBackground メソッド内に配置して、更新が 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);
}
==========================
WITH SWING WORKER バリアント:
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();
}