在java gui中更新Jlist问题

在java gui中更新Jlist问题,java,swing,user-interface,arraylist,jlist,Java,Swing,User Interface,Arraylist,Jlist,在我正在开发的应用程序中,我有一个用户基本上可以按姓名搜索客户的地方。。。在GUI中有一个文本字段,用户应在其中输入要搜索的客户名称和搜索按钮 当按下搜索按钮时,基本上会生成已找到客户的列表,从中填充JList,然后将其添加到外部面板。。。这是代码,我让它来说话 public void actionPerformed(ActionEvent e) { String nameToSearchWith = customerSearchInput.getText(

在我正在开发的应用程序中,我有一个用户基本上可以按姓名搜索客户的地方。。。在GUI中有一个文本字段,用户应在其中输入要搜索的客户名称和搜索按钮

当按下搜索按钮时,基本上会生成已找到客户的列表,从中填充JList,然后将其添加到外部面板。。。这是代码,我让它来说话

public void actionPerformed(ActionEvent e) 
    {      
        String nameToSearchWith = customerSearchInput.getText();

        Customer instance = new Customer();
        ArrayList<Customer> foundCustomers = instance.FindCustomersByName(nameToSearchWith);

        if (foundCustomers.size() > 0)
        {
            customerSearchInput.setText("");

            FoundCustomersListModel foundCustomersListModel = new FoundCustomersListModel(foundCustomers);
            JList foundCustomersList = new JList();
            foundCustomersList.setModel(foundCustomersListModel);
            foundCustomersList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
            foundCustomersList.setLayoutOrientation(JList.VERTICAL);
            foundCustomersList.setVisibleRowCount(-1);
            foundCustomersListModel.update();

            JScrollPane foundCustomersListScrollPanel = new JScrollPane(foundCustomersList);
            //foundCustomersListScrollPanel.setPreferredSize(new Dimension(250, 80));
            findCustomerPanel.add(foundCustomersListScrollPanel, BorderLayout.CENTER);

            JButton customerDeleteButton = new JButton("Delete Selected Customer");
            findCustomerPanel.add(customerDeleteButton, BorderLayout.SOUTH);
            customerDeleteButton.setEnabled(false);
            customerDeleteButton.addActionListener(new CustomerDeleteButtonListener(foundCustomersList, customerDeleteButton, foundCustomersListModel));

            foundCustomersList.addListSelectionListener(new CustomerListSelectionListener(foundCustomersList, customerDeleteButton));
            findCustomerPanel.revalidate();
        }
        else
        {   
            JOptionPane.showMessageDialog (null,
                "No customers where found with the given inputted name.",
                "Error",
                JOptionPane.ERROR_MESSAGE);
        }
    } 
public void actionPerformed(ActionEvent e)
{      
字符串名称ToSearchWith=customerSearchInput.getText();
客户实例=新客户();
ArrayList foundCustomers=instance.FindCustomersByName(nameToSearchWith);
如果(foundCustomers.size()>0)
{
customerSearchInput.setText(“”);
FoundCustomerListModel FoundCustomerListModel=新的FoundCustomerListModel(foundCustomers);
JList foundCustomersList=new JList();
setModel(FoundCustomerListModel);
FoundCustomerList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
FoundCustomerList.setLayoutOrientation(JList.VERTICAL);
FoundCustomerList.setVisibleRowCount(-1);
FoundCustomerListModel.update();
JScrollPane FoundCustomerListScrollPanel=新的JScrollPane(FoundCustomerList);
//FoundCustomerListScrollPanel.setPreferredSize(新维度(250,80));
添加(FoundCustomerListScrollPanel,BorderLayout.CENTER);
JButton customerDeleteButton=新JButton(“删除所选客户”);
添加(customerDeleteButton,BorderLayout.SOUTH);
customerDeleteButton.setEnabled(false);
addActionListener(新的CustomerDeleteButtonListener(FoundCustomerList、customerDeleteButton、FoundCustomerListModel));
FoundCustomerList.addListSelectionListener(新的CustomerListSelectionListener(FoundCustomerList,customerDeleteButton));
findCustomerPanel.revalidate();
}
其他的
{   
JOptionPane.showMessageDialog(空,
“未找到具有给定输入名称的客户。”,
“错误”,
JOptionPane.ERROR\u消息);
}
} 
ListModel类的代码:

public class FoundCustomersListModel extends AbstractListModel 
{       
    private ArrayList<Customer> foundCustomersList;

    public FoundCustomersListModel(ArrayList<Customer> foundCustomersList) 
    {
        this.foundCustomersList = foundCustomersList;
    }

    public void update()
    {
        this.fireContentsChanged(this, 0, (foundCustomersList.size() - 1));
    }

    @Override
    public Object getElementAt(int index) 
    {
        return(foundCustomersList.get(index));
    }

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

    @Override
    public void addListDataListener(ListDataListener l) {}

    @Override
    public void removeListDataListener(ListDataListener l) {}

    public void removeElementAt(int index) 
    {   
        foundCustomersList.remove(index);
        this.fireIntervalRemoved(this, index, index);
    }
}
公共类FoundCustomerListModel扩展了AbstractListModel
{       
私有ArrayList FoundCustomerList;
public FoundCustomerListModel(ArrayList FoundCustomerList)
{
this.foundCustomerList=foundCustomerList;
}
公共无效更新()
{
this.fireContentsChanged(this,0,(foundCustomerList.size()-1));
}
@凌驾
公共对象getElementAt(int索引)
{
return(foundCustomerList.get(index));
}
@凌驾
公共int getSize()
{
返回(foundCustomerList.size());
}
@凌驾
public void addListDataListener(ListDataListener l){}
@凌驾
public void removeListDataListener(ListDataListener l){}
公共void removeElementAt(int索引)
{   
FoundCustomerList.remove(索引);
this.fireIntervalRemoved(this,index,index);
}
}
我的问题是,例如,如果我搜索一个名为Fabian的客户,然后添加另一个名为Fabian的客户,然后再次搜索名为Fabian的客户,arraylist将正确更新,但JList不会更新,它仍将保持原样

然后经过大量的研究,我发现我应该使用fireContentsChanged(),因此我实现了更新方法,您可以在上面看到,它是有效的,但现在我遇到了另一个问题,也就是说,现在所有名为fabian的客户都被输出到JList中,但是在第一次搜索后添加的客户无法被选择。。。如果我按下按钮,什么都不会发生,它们将不会突出显示

此外,这非常奇怪,但如果我最小化应用程序,然后再次最大化,JList将恢复到原来的状态,即没有最后添加的客户

有人能帮我吗。。可能是因为线程并发性的缘故。。。我不知道说实话我对这方面不太了解


谢谢

您能发布一个完整编译的代码(可以复制粘贴到IDE中)吗?您可能应该对列表模型使用
addListDataListener
,而不是自己触发更改。您可以使用<代码> Debug TrimeStudio< /Cord>,而不是创建自己的.@ USE1803551或更好的,(最小的完整的和可验证的示例)。BTW——您应该考虑为此使用一个列表。为什么?因为
TableRowSorter
可以轻松地对行进行排序和过滤。