简体   繁体   English

JList Java的干净模型

[英]Clean model of a JList java

I try to fill a JList but first remove existing elements to avoid repeated records. 我尝试填充JList,但首先删除现有元素以避免重复记录。 LLenarGrid that method call on a button to show that is to display objects in arraylist and JList but if I have 5 elements and give twice the button I get 10 doubles me as if I did not clean up the model LLenarGrid该方法调用一个按钮以显示要在arraylist和JList中显示对象,但是如果我有5个元素并且给按钮两次,我得到10倍,这就像我没有清理模型一样

I leave my method, if I could help? 我可以离开我的方法吗? or that I'm doing wrong, Thanks 还是我做错了,谢谢

public void LlenarGrid()
{

    listapersonas.setModel(new DefaultListModel());
     DefaultListModel listModel = (DefaultListModel)listapersonas.getModel();
     listModel.removeAllElements();
     for (clsPersona d : personas) {
        listModel.addElement(d.RetornaPersona());
    }
    listapersonas.setModel(listModel);
    listapersonas.clearSelection();
}

You do not have to set the model to list multiple times. 您不必将模型设置为多次列出。

you can remove all element using below which you are already doing. 您可以使用下面已经删除的元素删除所有元素。

model.removeAllElements();

As suggested by John Bollinger check the personas List. 如John Bollinger所建议,请检查角色列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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