Animate Icon not work

Answered

 

hi, I'm a new developer and trying to make a demo, There was a JLabel and set icon(new com.intellij.ui.AnimatedIcon.Default()), but it seem to cant work animatedly.

button action listener:

pullButton.addActionListener(new AbstractAction("testListener") {
@Override
public void actionPerformed(ActionEvent e) {
pullDelayBar.setVisible(true); // Animated icon show
pullButton.setEnabled(false); // button disable
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(3000L);// just like remote call
pullDelayBar.setVisible(false);// Animated icon disappear
pullButton.setEnabled(true);// button enable
} catch (InterruptedException ex) {
throw new RuntimeException(ex);
}
}
});
}
});

Special instructions: remove code 'SwingUtilities.invokeLater(...)', it work normally.

  • IntelliJ IDEA 2022.2.3
  • ideaIC-2021.3.3-213.7172.25
  • JDK11

 

 

0
1 comment

Hi,

The code seems wrong to me because the button's `actionPerformed()` is executed on EDT already (by default in Swing) and you additionally execute a long-running task on EDT (inside `actionPerformed()`). A long-running task should be executed in the background and after it's finished you should update UI. Executing a long-running task in EDT will cause UI freeze.

Please take a look at https://plugins.jetbrains.com/docs/intellij/general-threading-rules.html.

0

Please sign in to leave a comment.