[英]How to dynamicly add elemnts to a JList / DefaultListModel
I'm want to dynamic add elements to my DefaultListModel
/ JList
but the list needs to emptied first. 我想将元素动态添加到
DefaultListModel
/ JList
但是列表需要先清空。 I do this opening an dialog-window . 我这样做是打开一个对话框窗口。 My problem is that when I use
model.removeAllElements()
my dialog-window reappears multiple times. 我的问题是,当我使用
model.removeAllElements()
对话框窗口会多次出现。 What am i doing wrong? 我究竟做错了什么?
I also tried model.addElementAt(index)
to bypass model.removeAllElements()
but the result is the same. 我还尝试了
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()
: open a dialog-window with 3 buttons when the user clicks the first one it connects to a URL then the List is updated by filllst()
. showTasksDialog()
:当用户单击第一个连接到URL的按钮时,打开带有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()
: should remove all elements from my default list and the refill it, but if I use model.removeAllElements()
the Dialog-windows reappears multiple times. filllist()
:应该从默认列表中删除所有元素并重新填充,但是如果我使用model.removeAllElements()
则对话框窗口会多次出现。 When I don't use removeAllElements
then everything is fine but the list is not emptied 当我不使用
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);
}
}
Remove listeners (or deactivate them) from the list before removing and adding elements, and then re-add the listeners when done. 在删除和添加元素之前,从列表中删除侦听器(或停用它们),然后在完成后重新添加侦听器。
eg, 例如,
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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.