简体   繁体   中英

jList not displaying data from custom model?

I'm trying to populate a JList through subclassing the AbstractListModel.I have looked through numerous places to try and find what I was doing wrong, but never managed to resolve the problem.So this class handles my 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

Then is my middle class...

 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();
    }
}

Then I have got the AbstractListModel implementation

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);    
}

.....

I tested DefautListModel and it displayed the values, but when I incorporate a custom model it doesn't display ? Is there a extra step I'm missing ? Also mainCtrl is the main class...

Thanks could really use some help

Instead of

fireIntervalAdded(this,0,getSize());

in your addContact() put

fireContentsChanged(this,0,getSize());

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


 list = new JList(listModel);

please refer this link it will help you

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM