Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Speeding up the loading of a JComboBox

Reply
Thread Tools

Speeding up the loading of a JComboBox

 
 
HP News Feed
Guest
Posts: n/a
 
      07-15-2005
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


 
Reply With Quote
 
 
 
 
Michael Rauscher
Guest
Posts: n/a
 
      07-15-2005
HP News Feed schrieb:
> 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.


JComboBox just delegates the message down to its model. So the problem
has to be located there.

>
> 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.


Who proposed this? I'd suggest to not use DefaultComboBoxModel at all.
It uses Vector and Vector is slow due to synchronization.

>
> 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.


Use your own implementation of ComboBoxModel. Implement a method
setData, an add method that is able to add complete collections etc.,
then you don't need to create a new model object every time.

Bye
Michael

 
Reply With Quote
 
 
 
 
HP News Feed
Guest
Posts: n/a
 
      07-15-2005
> Who proposed this? I'd suggest to not use DefaultComboBoxModel at all. It
> uses Vector and Vector is slow due to synchronization.


The proposal came from here :
http://java.sun.com/features/2002/03/swinggui.html

I am not actually using a Vector - this just came from the example. Mine
uses a List. I will take a look at your suggestion of implementing my own
ComboBoxModel. I am still interested in the legitamy of my original
proposal though.

Thanks,

Steve.


"Michael Rauscher" <(E-Mail Removed)> wrote in message
news:db89pp$tqf$02$(E-Mail Removed)-online.com...
> HP News Feed schrieb:
>> 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.

>
> JComboBox just delegates the message down to its model. So the problem has
> to be located there.
>
>>
>> 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.

>
> Who proposed this? I'd suggest to not use DefaultComboBoxModel at all. It
> uses Vector and Vector is slow due to synchronization.
>
>>
>> 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.

>
> Use your own implementation of ComboBoxModel. Implement a method setData,
> an add method that is able to add complete collections etc., then you
> don't need to create a new model object every time.
>
> Bye
> Michael
>



 
Reply With Quote
 
Michael Rauscher
Guest
Posts: n/a
 
      07-18-2005
HP News Feed schrieb:
>>Who proposed this? I'd suggest to not use DefaultComboBoxModel at all. It
>>uses Vector and Vector is slow due to synchronization.

>
>
> The proposal came from here :
> http://java.sun.com/features/2002/03/swinggui.html


This is just an example. No one proposed to use a DefaultComboBoxModel.
The key statement is to "perform operations in bulk to reduce the number
of events that are posted". This is independent of the model class one uses.

The statemenent "construct a new one instead of reusing the existing
one" that is given in this paper is valid only for models that don't
have the ability to replace the contents - like DCBM. Have a look at
DefaultTableModel (which also uses Vector). There's a method
setDataVector where one can replace the whole table contents.

>
> I am not actually using a Vector - this just came from the example. Mine
> uses a List. I will take a look at your suggestion of implementing my own


If you use DCBM then you use a Vector as DCBM uses a Vector.

> ComboBoxModel. I am still interested in the legitamy of my original
> proposal though.


The setModel approach is correct. If the model can't replace data it's
the only correct way. Of course, the garbage collector will properly
take care of the fact that you create new objects all the time. You can
call box.setModel(null) in advance to release the old model. Loading
this way won't affect the dynamic behaviour.

Bye
Michael

 
Reply With Quote
 
Michael Rauscher
Guest
Posts: n/a
 
      07-18-2005
Michael Rauscher schrieb:

> You can
> call box.setModel(null) in advance to release the old model.


Delete this. Of course, you can't set the model to null...

>
> Bye
> Michael
>


 
Reply With Quote
 
Joan
Guest
Posts: n/a
 
      07-18-2005

"Michael Rauscher" <(E-Mail Removed)> wrote in message
news:dbgi3a$vqq$05$(E-Mail Removed)-online.com...
> Michael Rauscher schrieb:
>
> > You can
> > call box.setModel(null) in advance to release the old model.

>
> Delete this. Of course, you can't set the model to null...


But you can set the Layout Manager to null <g>


 
Reply With Quote
 
Michael Rauscher
Guest
Posts: n/a
 
      07-19-2005
Joan wrote:
>>
>>>You can
>>>call box.setModel(null) in advance to release the old model.

>>
>>Delete this. Of course, you can't set the model to null...

>
>
> But you can set the Layout Manager to null <g>


???

I wrote the above with respect to garbage collection.

Bye
Michael
 
Reply With Quote
 
Andrew Thompson
Guest
Posts: n/a
 
      07-19-2005
On Tue, 19 Jul 2005 18:45:28 +0200, Michael Rauscher wrote:

> Joan wrote:
>>>
>>>>You can
>>>>call box.setModel(null) in advance to release the old model.
>>>
>>>Delete this. Of course, you can't set the model to null...

>>
>> But you can set the Layout Manager to null <g>

>
> ???
> I wrote the above with respect to garbage collection.


Hence the <g>.

I think that pun was more directed at folks that might
whine and complain every time someone sets a layout to
null, ..or about the quality of GUI advice delivered on
c.l.j.p.

Made me laugh!

--
Andrew Thompson
physci.org 1point1c.org javasaver.com lensescapes.com athompson.info
Beats A Hard Kick In The Face
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Loading a JComboBox bruce Java 11 11-23-2010 05:11 PM
Dynamic loading of a JComboBox bruce Java 4 10-18-2010 10:08 PM
Any tips on speeding up loading of websites during development? PhilipDaniels@foo.com ASP .Net 3 10-16-2007 06:04 PM
Speeding up loading? T.J. HTML 17 02-03-2005 12:09 AM
speeding up data transfer? Devin Panchal Wireless Networking 1 09-06-2004 05:46 PM



Advertisments