Search code examples
javaswingfor-loopjlistanimated-gif

For on JList setSelectedIndex


Im trying to through a JList by a for, the objective is to animate a Gif, in every item of the JList I have an Image, and when I push the Animate Button the for pass through every item on the JList and changes the Image on the Icon of the JLabel.

My problem is that I don't know how to use a for un a JList with the setSelectedIndex() and the getSelectedIndex().

Thanks for the help.

I was trying with:

on = true;
while(on){
    for(int i=0; i <=list.getSelectedIndex();i++){
            list.setSelectedIndex(i);
    }
}

Solution

  • First of all, don't use JList for animations. JLabels are much better options. Check this out, it explains how to use images.

    To answer your question, the for should be changed to iterate to list size (list.getModel().getSize()), and not to selected index, which is (obviously) the index of the selected item. Also, you might want a delay between each for iteration.