- Use a Swing Timer not a java.util.Timer as that's what it's for -- to give you timer functionality but on the Swing event thread.
- Your code, even if it worked would not do anything near what you desire as your for loop would loop immediately.
- You shouldn't be using a for loop at all. The Timer provides your looping functionality. Increment a counter in the timer and use its value to get the next icon. Set the counter to 0 when it reaches the size of the collection.
- What line causes your error, because it has nothing to do with the problem you describe.
separate thread to change label icon
-
23-09-2022 - |
Question
I want to use a separate thread for changing a label's icon every 2 seconds.The icons (pictures actually, reclama1, reclama2, etc) are stored in the project's folder called "poze". So when i try the code below, I receive the following error every 2 seconds. What am I doing wrong?
error:
java.lang.RuntimeException: Uncompilable source code - Erroneous tree type:<any>
code:
public GUI() {
initComponents();
JTable.setModel(m);
JTable.setRowSorter(sorter);
bFiltru.setVisible(false);
bFiltru.setText("Afiseaza intreaga lista");
TimerTask task = new TimerTask(){
public void run(){
for (int i=1; i<5, i++){
ImageIcon iconLogo = new ImageIcon("poze/reclama"+i+".gif");
jLabelReclama.setIcon(iconLogo);
if (i == 4) i = 0;
}
}
}
Timer t = new Timer();
t.schedule(task, 0, 2000);
}
La solution
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow