Speeding up the loading of a JComboBox

Discussion in 'Java' started by HP News Feed, Jul 15, 2005.

  1. HP News Feed

    HP News Feed Guest

    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
    HP News Feed, Jul 15, 2005
    #1
    1. Advertising

  2. 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
    Michael Rauscher, Jul 15, 2005
    #2
    1. Advertising

  3. HP News Feed

    HP News Feed Guest

    > 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" <> wrote in message
    news:db89pp$tqf$02$-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
    >
    HP News Feed, Jul 15, 2005
    #3
  4. 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
    Michael Rauscher, Jul 18, 2005
    #4
  5. 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
    >
    Michael Rauscher, Jul 18, 2005
    #5
  6. HP News Feed

    Joan Guest

    "Michael Rauscher" <> wrote in message
    news:dbgi3a$vqq$05$-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>
    Joan, Jul 18, 2005
    #6
  7. 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
    Michael Rauscher, Jul 19, 2005
    #7
  8. 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
    Andrew Thompson, Jul 19, 2005
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Spamtrap

    Need some hints on speeding up

    Spamtrap, Aug 11, 2004, in forum: Perl
    Replies:
    1
    Views:
    370
    Jim Gibson
    Aug 12, 2004
  2. T.J.

    Speeding up loading?

    T.J., Jan 31, 2005, in forum: HTML
    Replies:
    17
    Views:
    671
    Henry
    Feb 3, 2005
  3. Replies:
    3
    Views:
    287
    George Ter-Saakov
    Oct 16, 2007
  4. bruce
    Replies:
    4
    Views:
    1,265
    bruce
    Oct 18, 2010
  5. bruce

    Loading a JComboBox

    bruce, Nov 21, 2010, in forum: Java
    Replies:
    11
    Views:
    1,068
    Martin Gregorie
    Nov 23, 2010
Loading...

Share This Page