I am trying to change JList rows dynamically. I need change nth row colour, highlight it(n is unknown during compilation). I saw a lot of examples with custom ListCellRender
Your custom ListCellRenderer, which implements the method getListCellRendererComponent
, will have access to both the JList
and the value that it is redering. This gives you a couple options for how to determine when to paint the nth row green:
JList
and have the renderer ask it which color to use for the bg. The JList
subclass could trigger a repaint when the business logic determines that it is time for the nth row to be green, and then start an Swing Timer to trigger a repaint returning the bg back to normalgetListCellRendererComponent
, setting the bg green if the state is correct. Again, you have the option of setting an Swing Timer
to revert the state on the backing object.