1

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);
    }
}
Zakaria Acharki
  • 66,747
  • 15
  • 75
  • 101
  • Sounds like you want to use something like a Swing Timer to increment your selection for you. – Kevin Workman Jan 09 '14 at 17:03
  • 1) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve). 2) One way to get image(s) for an example is to hot-link to the images seen in [this answer](http://stackoverflow.com/a/19209651/418556). – Andrew Thompson Jan 09 '14 at 17:36

1 Answers1

2

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.

rhobincu
  • 906
  • 1
  • 7
  • 22
  • I know, but its only for do an Example. The problem is that I need a Thread for update the Jframe that im drawing every time the selectedIndex is change it. – Marc Carmona Jan 09 '14 at 17:48