You can animate the tray icon by providing a sequence of images (frames). This is useful for indicating background processing.
1. Prepare the frames
Create a LinkedList<File> or LinkedList<Image> containing the frames in sequence. It is recommended to sort files by name if loading from a directory.
File[] files = new File("path/to/my/imageFiles").listFiles();
LinkedList<File> fileList = new LinkedList<>(Arrays.asList(files));
fileList.sort(Comparator.comparing(File::getName));
2. Apply the animation
Use the Builder or call .newAnimation() on an existing instance. The integer parameter specifies the frame duration in milliseconds.
Via Builder:
trayIcon = new FXTrayIcon.Builder(primaryStage, iconFile)
.animate(fileList, 75)
.addExitMenuItem("Exit", e -> Main.stopRunning())
.show()
.build();
After instantiation:
trayIcon.newAnimation(fileList, 75);
3. Control the animation
Use the following methods to manage playback:
play(): Start animation.playFromStart(): Restart animation.stop(): Stop animation.pause(): Pause animation.pauseResume(): Toggle pause/resume.stopReset(): Stop and reset.resetIcon(): Reset to the original static icon.
4. Check animation state
isRunning()isPaused()isStopped()getTimeline(): Access the underlying animation timeline.
File[] files = new File("path/to/my/imageFiles").listFiles();
LinkedList<File> fileList = new LinkedList<>(Arrays.asList(files));
fileList.sort(Comparator.comparing(File::getName));
trayIcon = new FXTrayIcon.Builder(primaryStage, iconFile)
.animate(fileList, 75)
.addExitMenuItem("Exit", e -> Main.stopRunning())
.show()
.build();