1

这是我的代码,变量bq是一个名为BasicQuery的自定义类,它返回一个JavaDB连接...... AutoCompleteDecorator是swingX库中用于实现自动完成功能的类......这个代码当rus大约3次运行正常但之后它一直被冻结,一段时间后抛出内存不足异常!我找不到问题出在哪里。谁能帮帮我吗?另外,如果您需要代码的其他部分,请告诉我!

private void initCombos()
{
    ResultSet r=bq.executeQuery("select productID,productName from products");
    cmbProductID.removeActionListener(this);
    cmbProductID.removeActionListener(this);

    try
    {
        cmbProductID.removeAllItems();
        cmbProductName.removeAllItems();
        cmbCodes.removeAllItems();
        String s1;
        while(r.next())
        {
            s1=r.getString(1).trim();
            cmbProductID.addItem(s1);
            cmbCodes.addItem(s1);
            cmbProductName.addItem(r.getString(2).trim());
        }
        r.close();
        cmbProductID.addActionListener(this);
        cmbProductName.addActionListener(this);
        AutoCompleteDecorator.decorate(cmbProductID);
        AutoCompleteDecorator.decorate(cmbProductName);
    }
    catch(Exception x)
    {
        JOptionPane.showMessageDialog(this,"Error setting up ComboBoxes "+x);
    }
}

4

2 回答 2

2

在这个部分:

ResultSet r=bq.executeQuery("select productID,productName from products");
cmbProductID.removeActionListener(this);
cmbProductID.removeActionListener(this);

我想你的意思是:

ResultSet r=bq.executeQuery("select productID,productName from products");
cmbProductID.removeActionListener(this);
cmbProductName.removeActionListener(this);

否则,有一个事件处理程序保持打开状态cmbProductName会泄漏内存。

于 2010-02-13T16:10:09.127 回答
2

我看到了两个问题。@Nick Carver 指出了一个(+1)。如果您没有按照您的意图删除 ActionListener cmbProductName,您将在每次迭代中从该组合传播动作事件ResultSet

第二个是你似乎在你的绘画线程中做了一些繁重的工作。您的数据库调用通常应该在工作线程中进行,并且只有绘制 gui 的任务才能在绘制线程中进行。否则你会阻塞这个线程并且你的应用程序会感觉迟钝。查看SwingWorker和 Swing 中有关并发的概念,以获取有关此主题的更多信息

于 2010-02-13T16:34:32.900 回答