簡體   English   中英

jList不顯示來自自定義模型的數據?

[英]jList not displaying data from custom model?

我正在嘗試通過對AbstractListModel進行子類化來填充JList。我已經遍歷許多地方來嘗試找出我做錯了什么,但從未設法解決問題。因此此類處理了我的GUI ...

//View
public class central extends javax.swing.JFrame {

public central() {
    initComponents();
    list.addMouseListener(new abstracts.mouseActions(list));

}

public void setListModel(ListModel l ){
    list.setModel(l);

}

// The rest are auto generated code for the interface, not relevant

那是我的中產階級...

 public class MainCtrl {
//View reference
private views.central mainFrame = new views.central();
//Model reference
private abstracts.ListData model = new abstracts.ListData();
/*All this was testing purposes and it worked
  private DefaultListModel model = new DefaultListModel();
 */

private void showView(){
    mainFrame.setListModel(model);
    mainFrame.setVisible(true);
    models.contact p2 = new models.contact("Alex", "Christopher","alex@hotmail.com","22","Def");
    models.contact p1 = new models.contact("Joes", "Smith","joey@hotmail.com","33","Def");
    model.addContact(p2);
    model.addContact(p1);
    /* def version
      model.addElement(p2);

    */


}

public static void main(String[] args) {
    try {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        new MainCtrl().showView();
    }
    catch (Exception e) {
        e.printStackTrace();
    }
}

然后我有了AbstractListModel實現

public class ListData extends AbstractListModel {    
//Store people info
private Vector<contact> people; 

public ListData() {
    people = new Vector<contact>();
}

public void addContact(contact newPerson){
   people.add(newPerson);
   int per = people.indexOf(newPerson);
   fireIntervalAdded(this,0,getSize());
}

@Override
public contact getElementAt(int index){
    return  people.get(index);
}

@Override
public int getSize(){
    return people.size();
}


@Override
protected void fireIntervalAdded(Object src, int index, int index2){
 System.out.println(index2);    
}

.....

我測試了DefautListModel,它顯示了值,但是當我合並自定義模型時,它不顯示嗎? 我還缺少其他步驟嗎? mainCtrl也是主類...

謝謝,真的可以使用一些幫助

代替

fireIntervalAdded(this,0,getSize());

在您的addContact()中

fireContentsChanged(this,0,getSize());

 listModel = new DefaultListModel();
 listModel.addElement("Jane Doe");
 listModel.addElement("John Smith");
 listModel.addElement("Kathy Green");


 list = new JList(listModel);

請參考此鏈接,它將為您提供幫助

http://docs.oracle.com/javase/tutorial/uiswing/components/list.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM