H
HP News Feed
I have seen various discussion entries about the poor performance of the
JComboBox additem method when there are a large number of items in the
control.
The proposed route to improvement is invariably to use a
DefaultComboBoxModel and then create the JComboBox control using the
constructor that uses the Model. e.g.
Vector v = new Vector(numItems);
for (int i = 0; i < numItems; i++) {
v.add(new Integer(i));
}
DefaultComboBoxModel model = new DefaultComboBoxModel(v);
JComboBox box = new JComboBox(model);
I support an application that is showing symptoms of this problem, and I
have experimented with the above technique, and it appears to work well.
However, I want to be able to re-populate the item list of an existing empty
JComboBox i.e. I do not have the opportunity to invoke the model
contstructor while creating a new JComboBox. Instead I have used the
setModel method. e.g.
private void populateCombo(int numItems)
{
Vector v = new Vector(numItems);
for (int i = 0; i < numItems; i++) {
v.add(new Integer(i));
}
DefaultComboBoxModel model = new DefaultComboBoxModel(v);
box.setModel(model);
}
I am a newcomer to java and want to ask if the above is a legitimate
approach. e.g. Will the java garbage collection capabilities properly take
care of the fact that a new DefaultComboBoxModel is created on every call to
this method ? Will loading it in this way alter it's dynamic behaviour
later in it's life (e.g. the firing of events as the list is navigated and
items are selected).
Thanks,
Steve Shadbolt, HP UK
Remove hat to reply
JComboBox additem method when there are a large number of items in the
control.
The proposed route to improvement is invariably to use a
DefaultComboBoxModel and then create the JComboBox control using the
constructor that uses the Model. e.g.
Vector v = new Vector(numItems);
for (int i = 0; i < numItems; i++) {
v.add(new Integer(i));
}
DefaultComboBoxModel model = new DefaultComboBoxModel(v);
JComboBox box = new JComboBox(model);
I support an application that is showing symptoms of this problem, and I
have experimented with the above technique, and it appears to work well.
However, I want to be able to re-populate the item list of an existing empty
JComboBox i.e. I do not have the opportunity to invoke the model
contstructor while creating a new JComboBox. Instead I have used the
setModel method. e.g.
private void populateCombo(int numItems)
{
Vector v = new Vector(numItems);
for (int i = 0; i < numItems; i++) {
v.add(new Integer(i));
}
DefaultComboBoxModel model = new DefaultComboBoxModel(v);
box.setModel(model);
}
I am a newcomer to java and want to ask if the above is a legitimate
approach. e.g. Will the java garbage collection capabilities properly take
care of the fact that a new DefaultComboBoxModel is created on every call to
this method ? Will loading it in this way alter it's dynamic behaviour
later in it's life (e.g. the firing of events as the list is navigated and
items are selected).
Thanks,
Steve Shadbolt, HP UK
Remove hat to reply