0

我有一个名为 jListCustSearchResults 的 jList 对象,它包含许多 CustomerEntity 对象并将它们显示为用户选择客户的列表。

下面的第一个方法是一个 JButton 的 actionperformed 方法,它在单击时触发此 JList 的更新序列。它调用下面名为 fillCustomerList 的另一个函数,用从数据库中检索到的新客户重新填充 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);
}

===========================

使用 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();
}
4

0 回答 0