我試圖在按下按鈕時更新JList的內容。所以,我清除了列表模型,然後清除了列表,然後繼續向列表中添加新值。這裏是剝離代碼:按鈕上的JList的Occational更新按
testList.java
public class testList extends javax.swing.JFrame {
private Thread t;
public DefaultListModel model;
public boolean first = true;
public testList() {
model = new DefaultListModel();
initComponents();
this.centre(this);
}
public static void centre(javax.swing.JFrame f) {
Dimension us = f.getSize(), them = Toolkit.getDefaultToolkit().getScreenSize();
int newX = (them.width - us.width)/2;
int newY = (them.height - us.height)/2;
f.setLocation(newX, newY);
}
class updateList implements Runnable {
public void run() {
tmp.getTheList();
model.clear();
ouputList.removeAll();
for (int i = 0; i < tmp.returnList.size(); i++) {
model.addElement(tmp.returnList.get(i));
}
if (first) {
chList.setModel(model);
}
}
}
private void initComponents() {
// generated by NetBeans 6.9
}
private void buttonActionPerformed(java.awt.event.ActionEvent evt) {
t = new Thread(new updateList(), "List Updater");
t.start();
}
public static void main(String args[]) {
tmp = new aC();
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new chapList().setVisible(true);
}
});
}
static aC tmp;
private javax.swing.JButton button;
public static javax.swing.JList outputList;
private javax.swing.JScrollPane jScrollPane1;
}
ac.java
public class aC extends testList {
ArrayList returnList = new ArrayList();
void getTheList() {
returnList.clear();
generateList();
}
void generateList() {
// populate returnList with random values of random size using returnlist.add()
}
}
我面臨的問題是,當列表中首次創建,它更新JList。當再次按下按鈕時,JList有時會被更新。爲了進一步按下按鈕,JList中不顯示任何內容。
有人可以幫我找出是什麼原因導致這個問題? 謝謝。
如果你被允許,用大寫字母開始你的班名。這是大多數Java開發人員使用的慣例,因此它使得許多Java開發人員更容易閱讀您的代碼。 – 2010-11-19 01:18:48
謝謝你讓我知道。我會改變類名。 – Monk 2010-11-19 01:34:43