Gif animé dans un JButton
Salut à toutes et à tous
Dans mon application, je voudrais mettre un gif animé sur un bouton pour illustrer la progression d'une tâche.
Alors j'arrive à mettre l'ImageIcon, mais voilà... ça bug.
1. L'animation va beaucoup trop vite
2. Au survol du bouton, l'image disparait.
Quelqu'un a déjà rencontré ce souci ?
Voici mon code, sait-on jamais.
Code:
1 2
| act = new JButton("Deactivate all plugins");
act.setIcon(IconFinder.getIcon("progress")); |
Et l'IconFinder
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| public class IconFinder {
private static Map<String, String> names;
private static Map<String, ImageIcon> icons;
static {
names = new TreeMap<String, String>();
icons = new TreeMap<String, ImageIcon>();
names.put("cancel", "cancel.png");
names.put("run", "cog.png");
names.put("progress", "progress.gif");
}
public static ImageIcon getIcon(String name) {
if (icons.containsKey(name)) {
return icons.get(name);
}
else if (names.containsKey(name)) {
return loadIcon(name);
}
else {
throw new InternalError("No icon named " + name);
}
}
private static ImageIcon loadIcon(String name) {
String fileName = "icons/" + names.get(name);
URL url = IconFinder.class.getResource(fileName);
if (url != null) {
ImageIcon icon = new ImageIcon(url);
icons.put(name, icon);
return icon;
}
else {
throw new InternalError("An icon is missing from the JAR");
}
}
} |