Skip to main content

AutoCompleteDecorator memory leak in JComboBoxes

4 replies [Last post]
tj89
Offline
Joined: 2010-02-14

This is my code here the variable bq is a custom class called BasicQuery it returns a JavaDB connection... 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! It seems like the bug i found in swinglabs

https://swingx.dev.java.net/issues/show_bug.cgi?id=299

But according to that the bug has been fixed I'm using SwingX1.6 release.

private void initCombos()
{
ResultSet r=bq.executeQuery("select productID,productName from products");
cmbProductID.removeActionListener(this);
cmbProductName.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);
}
}

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
kschaefe
Offline
Joined: 2006-06-08

Well, there are a unit tests for this bug in AutoCompleteDecoratorTest. Without a small, runnable demo, I would assume that the tests are accurate. Please provide such a demo if you are still experiencing the problem.

Karl

tj89
Offline
Joined: 2010-02-14

Ok here is the code for a small test app... Yes I am still experiencing the problem whenever i redecorate the combo boxes about 5 or 6 times it will leak memory. Please tell me if my usage of the AutoCmpleteDecorator is wrong.
Thanks,
Thihara.

import org.jdesktop.swingx.autocomplete.AutoCompleteDecorator;
/*
* AutoComplete.java
*
* Created on February 19, 2010, 10:25 AM
*/

/**
*
* @author TJ
*/
public class AutoComplete extends javax.swing.JFrame
{

/** Creates new form AutoComplete */
public AutoComplete()
{
initComponents();

}

private void initComponents()
{
btnRefresh = new javax.swing.JButton();
cmb = new javax.swing.JComboBox();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
btnRefresh.setText("Refresh");
btnRefresh.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent evt)
{
btnRefreshActionPerformed(evt);
}
});

cmb.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGap(20, 20, 20)
.addComponent(cmb, javax.swing.GroupLayout.PREFERRED_SIZE, 217, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 36, Short.MAX_VALUE)
.addComponent(btnRefresh)
.addGap(56, 56, 56))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(54, 54, 54)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btnRefresh)
.addComponent(cmb, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(223, Short.MAX_VALUE))
);
pack();
}

[b] //This is where i call the AutoCompleteDecorator.decorate(cmb);
private void btnRefreshActionPerformed(java.awt.event.ActionEvent evt)
{
cmb.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4", "ITTTTM" }));
[u]AutoCompleteDecorator.decorate(cmb);[/u]
}[/b]

/**
* @param args the command line arguments
*/
public static void main(String args[])
{
java.awt.EventQueue.invokeLater(new Runnable()
{
public void run()
{
new AutoComplete().setVisible(true);
}
});
}

private javax.swing.JButton btnRefresh;
private javax.swing.JComboBox cmb;

}

kschaefe
Offline
Joined: 2006-06-08

Yeah, theres a bug in there. The original bug was related to a leak with the listeners, but we still have a leak with the adapter (just not as bad). Please file a new bug, referencing this thread.

Thanks,

Karl

tj89
Offline
Joined: 2010-02-14

I'm sorry for taking so long to reply. I found the solution for this problem, It seems that I made a mistake in my code

[b]private void btnRefreshActionPerformed(java.awt.event.ActionEvent evt)
{
cmb.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4", "ITTTTM" }));
AutoCompleteDecorator.decorate(cmb);
}[/b]

Even if I don't decorate the combo box when the data changes in it the auto complete list is updated and works fine.

So I just called

[b] AutoCompleteDecorator.decorate(cmb);[/b]

in the constructer and no probelemo... sorry for the confusion.
Cheers!