如何动态地将元素添加到JList / DefaultListModel

时间:2015-10-04 11:56:08

标签: java swing jlist defaultlistmodel

我想动态添加元素到我的DefaultListModel / JList,但列表需要先清空。我这样打开一个对话窗口。我的问题是,当我使用model.removeAllElements()时,我的对话窗口会重新出现多次。我做错了什么?

我还尝试model.addElementAt(index)绕过model.removeAllElements(),但结果是一样的。

private javax.swing.JList serviceList;
serviceList.setModel(model);
serviceList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
serviceList.setLayoutOrientation(javax.swing.JList.HORIZONTAL_WRAP);
serviceList.setSelectionBackground(java.awt.Color.white);
serviceList.setVisibleRowCount(3);
serviceList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
        public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
            serviceListValueChanged(evt);
        }
    });

 private void serviceListValueChanged(javax.swing.event.ListSelectionEvent evt) {                                         
    showTasksDialog();
}

showTasksDialog():当用户点击连接到网址的第一个按钮时,打开一个包含3个按钮的对话窗口,然后按filllst()更新列表。

public void showTasksDialog() {
    int selection = serviceList.getSelectedIndex();
    Object[] options = {"Analyse", "Build", "Stop"};
    int n = taskDialog.showOptionDialog(this,
            "What should this Service do?",
            "",
            JOptionPane.YES_NO_CANCEL_OPTION,
            JOptionPane.QUESTION_MESSAGE,
            null,
            options,
            null);
    if (n == 0) {
        try {
            connection.setSlaveToAnalyse(serviceURLJSONArray.getString(selection));
            filllist();
        } catch (JSONException | IOException ex) {
            Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

filllist():应该从我的默认列表中删除所有元素并重新填充它,但是如果我使用model.removeAllElements(),则Dialog-windows会重新出现多次。当我不使用removeAllElements时,一切都很好,但列表没有清空

public void filllist() throws JSONException, IOException {        
    model.removeAllElements();
    serviceURLJSONArray = connection.getSlaves();
    for (int i = 0; i < serviceURLJSONArray.length(); i++) {
        String slaveStatus = new Connection().getSlaveStatus(serviceURLJSONArray.getString(i));
        model.addElement("Service " +(i+1)+" "+slaveStatus);
    }
}

1 个答案:

答案 0 :(得分:2)

在删除和添加元素之前从列表中删除侦听器(或取消激活它们),然后在完成后重新添加侦听器。

如,

public void filllist() throws JSONException, IOException {        

    // remove all listeners
    ListSelectionListener[] listeners = serviceList.getListSelectionListeners();
    for (ListSelectionListener l : listeners) {
        serviceList.removeListSelectionListener(l);
    }

    // do your work
    model.removeAllElements();
    serviceURLJSONArray = connection.getSlaves();
    for (int i = 0; i < serviceURLJSONArray.length(); i++) {
        String slaveStatus = new Connection().getSlaveStatus(serviceURLJSONArray.getString(i));
        model.addElement("Service " +(i+1)+" "+slaveStatus);
    }

    // add them back
    for (ListSelectionListener l : listeners) {
        serviceList.addListSelectionListener(l);
    }

}