gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 09:30:35 27 4
gpt4 key购买 nike

我正在尝试通过子类化 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());

关于java - jList 不显示来自自定义模型的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16263649/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com