Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

excessive load time building large JList

RichFFeb 10 2013 — edited Feb 10 2013
I have an [url http://r0k.us/graphics/SIHwheel.html]application using a JList. The size is variable, and can be reloaded with a different list of things. Up to now, the max list size has been about 1600, and (re)initializations have been quick, a matter of a few seconds for the 1600-element list.

Now I'd like to work with larger lists, one with over 4,000 elements, and one with over 60,000. The 4,000 element one loads in about 20 seconds (significantly more than double the 1,600-element list). The 60,000-element one takes many hours -- so many I have not allowed it to complete. The problem is, the more elements there are, the longer it takes to add a new element.

Here is a simplified version of the list re-initialization. Note that "model" is the data represented by the list.
class simpler
{
    final DefaultListModel model = new DefaultListModel();
    private JList               list;
// following line in GUI initialization
//        list = new JList(model);

    private void getSortedNames(int sortCmd)
    {
        int howbig = 50000;
        ColorName worker;
        String hexName[];

        // clear-out old elements
        list.clearSelection();
        model.removeAllElements();  // without this line, even small lists take long time
        model.ensureCapacity(howbig);  // just added hoping it would help.  It doesn't.

        for (int i = 0; i < howbig; i++)
        {
if ((i % 16) == 1) System.out.print(i + " ");
            hexName = new String[2];
//            worker = ColorName.findColorName(i);
//            hexName[0] = worker.getHex();
//            hexName[1] = worker.getName();
hexName[0] = Integer.toString(i);
hexName[1] = Integer.toString(i + 1);
            model.addElement(hexName);
        }
    }
}
The lines within the loop which start on the first column are debug stuff. I wasn't sure whether the slowdown was due to findColorName() or addElement(), so I took findColorName() out of play. No effect on speed, so I'm pretty sure it is within the addElement() voodoo where the progressive slowdown problem lies.

If I do not create a new hexName[] each iteration, the process is extremely quick. Of course, that creates 50,000 list items of the same instance of the same data.

After studying [url http://docs.oracle.com/javase/1.4.2/docs/api/javax/swing/DefaultListModel.html]Class DefaultListModel, I tried replacing model.addElements(hexName) with both model.set(i, hexName) and model.setElementAt(hexName, i) . Both led to runtime errors, with the Jlist crashing. (In some cases the rest of the program would keep going.) I'm not sure what the difference is between the set() and setElementAt() is, but neither worked. I was hoping with the whole JList pre-allocated ahead of time, it would be quicker to set by index rather than add-at-end. Maybe it would be if the operation could complete without crashing.

Does anyone have an idea how to speed this up?
This post has been answered by sabre150 on Feb 10 2013
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 10 2013
Added on Feb 10 2013
8 comments
411 views