Take the 2-minute tour ×

I've achieved what I'm trying to do, but I can't help but to think there is a more efficient way...allow me to illustrate.

In short, the question I'm asking is if there's a way to determine when a component has finished it's initial rendering.

I have a JList, which is hooked up to a DefaultListModel and getting painted by a custom renderer which extends the DefaultListCellRenderer.

This intention of the JList is to "page" through a log file, populating 2500 elements every time a new page is loaded. On my machine it usually takes a couple seconds to fully render the JList, which is not really a problem because changing the cursor to a wait cursor would be acceptable because it would give the user immediate feedback. Unfortunately, I cannot figure out an elegant way to know when the initial rendering has completed.

Below is the code of my renderer, in it you'll see I'm counting the number of iterations on the renderer. If that is between 0 and N-20, the cursor changes to a wait cursor. Once N-20 has been reached, it reverts back to a default cursor. Like I previously mentioned, this works just fine, but the solution really feels like a hack. I've implemented the ListDataListener of the DefaultListModel and the PropertyChangeListener of the JList, but neither produce the functionality I'm looking for.

 * Renders the MessageHistory List based on the search text and processed events
public class MessageListRenderer extends DefaultListCellRenderer
    private static final long serialVersionUID = 1L;

    String lineCountWidth = Integer.toString(Integer.toString(m_MaxLineCount).length());

    public Component getListCellRendererComponent(JList l, Object value, int index, boolean isSelected, boolean haveFocus) 
        JLabel retVal = (JLabel)super.getListCellRendererComponent(l, value, index, isSelected, haveFocus);
        retVal.setText(formatListBoxOutput((String)value, index));

        // initial rendering is beginning - change the cursor
        if ((renderCounter == 0) && !renderCursorIsWait)
            renderCursorIsWait = true;

        // initial rendering is complete - change the cursor back
        if ((renderCounter > (l.getModel().getSize() - 20)) && renderCursorIsWait)
            renderCursorIsWait = false;


        return retVal;

     * Adds font tags (marks as red) around all values which match the search criteria
     * @param lineValue string to search in 
     * @param lineIndex line number being processed
     * @return string containing the markup
    private String formatListBoxOutput(String lineValue, int lineIndex)
        // Theoretically the count should never be zero or less, but this will avoid an exception just in case
        if (m_MaxLineCount <= 0)
            return lineValue;

        String formattedLineNumber = String.format("%" + Integer.toString(Integer.toString(m_MaxLineCount).length()) + "s", m_CurrentPage.getStartLineNumber() + lineIndex) + ": ";

        // We're not searching, simply return the line plus the added line number
        if((m_lastSearchText == null) || m_lastSearchText.isEmpty())
            return "<html><font color=\"#4682B4\">" + formattedLineNumber.replaceAll(" ", "&nbsp;") + "</font>" + lineValue + "</html>";

        // break up the search string by the search value in case there are multiple entries 
        String outText = "";    
        String[] listValues = lineValue.split(m_lastSearchText);

        if(listValues.length > 1)
            // HTML gets rid of the preceding whitespace, so change it to the HTML code

            for(int i = 0; i < listValues.length; i++)
                outText += listValues[i];
                if(i + 1 < listValues.length)
                    outText += "<font color=\"red\">" + m_lastSearchText + "</font>";

            return outText + "</html>";


All I'm doing to populate the model is this:

// reset the rendering counter
this.renderCounter = 0;

// Reset the message history and add all new values
for (int i = 0; i < this.m_CurrentPage.getLines().size(); i++)
share|improve this question
Could you provide more code? –  StanislavL Sep 27 '11 at 19:14
More code was added, but I'm not sure if it helps with the question. What I'm looking for is a way to determine if the component is finished with it's initial rendering without implementing a counter. –  JoshBramlett Sep 27 '11 at 19:43
never ever (as in: nononono, simply dont) change the state of the calling list in the renderer. All parameters given in the getXXRendererComponent are strictly read-only. Move your logic somewhere else as @trashgod already suggested –  kleopatra Sep 28 '11 at 7:08

2 Answers 2

up vote 3 down vote accepted

1) you have add Cursor to the JList, not inside Renderer, example here or here

2) Renderer by default returns JLabel, there isn't special reason for defining that

3) Renderer can setBackground, Font whatever methods for (in this case) the JLabel


4) remove retVal.setText(formatListBoxOutput((String)value, index)); create DefaulListModel and move prepared Items to the Model#addItem

5) int index, can compare with JList#getModel().getSize() -1; remove that from Renderer,

6) better would be add your Items from BackGround Task (@trashgod your suggestion was right:-) and fill in batches (thank you), if you'll implemnts SwingWorker, then you can create batch at 50 Items and adding by 50... until done

7) Renderer is only for formating output, your Html formating could be:

  • removed, rest see in point 3th.
  • prepared in BackGroung Task instead of built constructor between Renderer and loading + Html Formating + Backward interactions to the Renderer, in this case is Renderer useless, because you can pertty pre-formating Items that by using Html, then remove Renderer
share|improve this answer
Thank you, but I believe you misinterpreted the question. The cursor is getting set on the parent window (via the global variable m_This). Like I said in the post, my implementation works, I'm just simply curious if there is a way to elegantly determine when the initial rendering of the JList has completed. –  JoshBramlett Sep 27 '11 at 18:27
please see my edit, @trashgod thanks for kick –  mKorbel Sep 27 '11 at 22:35
I agree; as much as possible, try to move any loading/formating out of the renderer and into the worker thread. –  trashgod Sep 27 '11 at 22:56
@Josh: Kudos for accepting this comprehensive answer. I would encourage you to up-vote it as well. –  trashgod Oct 14 '11 at 0:58

In addition to @mKorbel suggestions, use a SwingWorker to fill the list model in batches. Add a property change listener to the worker and restore the cursor when done. There's a related Q&A here.

private static class TaskListener implements PropertyChangeListener {

    public void propertyChange(PropertyChangeEvent e) {
        if (e.getNewValue() == SwingWorker.StateValue.DONE) {
            // restore cursor
share|improve this answer
Sorry for the false start; SwingWorker notifies PropertyChangeListeners on the event dispatch thread. –  trashgod Sep 27 '11 at 21:46
Hey guys, just wanted give a heads up I'm not abandoning this...just got put on a more urgent problem and I haven't had a chance to go through your suggestions. Thanks again for all the help. –  JoshBramlett Oct 1 '11 at 5:06
Thanks guys, this is exactly what I was looking for, and more importantly I have a much better understanding of what was going on. –  JoshBramlett Oct 3 '11 at 19:56

Your Answer


