This is my code here the variable bq is a custom class called BasicQuery it returns a JavaDB connection… AutoCompleteDecorator is the class from swingX library used to implement the autocomplete functions… This code when rus about 3 times run properly but after that it keep getting frozen and after a while throws the out of memory exception! I cant find where the problem is. Can anyone please help me? Also if you need other parts of the code please let me know!
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);
}
}
There are two problems that I see. @Nick Carver pointed one out (+1). If you dont remove the ActionListener from
cmbProductNameas you no doubt intended, you will be propagating action events from that combo on every iteration over yourResultSet.The second is that you appear to be doing some heavy lifting in your painting thread. Your db calls should in general be made in a worker thread, and only the tasks that paint the gui be made in the painting thread. Otherwise you will be blocking this thread and your application will feel sluggish. Look at SwingWorker and concepts around concurrency in Swing for more info on this topic.