jList不显示来自自定义模型的数据



我正试图通过对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

相关内容

  • 没有找到相关文章

最新更新