6

Like when you're downloading something from Chrome the taskbar icon shows like this:

windows taskbar

i would like to show the progress of my program in the taskbar like that. how can you do this using javaFX...?

Yago Rodriguez
  • 354
  • 1
  • 10
  • Maybe through the [`SystemTray`](https://docs.oracle.com/javase/8/docs/api/java/awt/SystemTray.html) class. – Andy Turner Aug 28 '16 at 14:55
  • 1
    Possible duplicate of [Windows 7 Taskbar Progress Bar in Java](http://stackoverflow.com/questions/2167037/windows-7-taskbar-progress-bar-in-java) – brian Aug 28 '16 at 15:56

2 Answers2

7

hope this way helps

package so;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import javax.imageio.ImageIO;

import javafx.application.Application;
import javafx.embed.swing.SwingFXUtils;
import javafx.scene.Scene;
import javafx.scene.SnapshotParameters;
import javafx.scene.control.Button;
import javafx.scene.control.ProgressBar;
import javafx.scene.image.Image;
import javafx.scene.image.WritableImage;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class JavaFXExample extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    private static final int ICONSIZE = 16;

    ProgressBar bar = new ProgressBar(0.2d);

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("change taskbar icon");
        Button btn = new Button();
        btn.setText("click me several times while looking at task bar");

        bar.setMaxWidth(ICONSIZE);

        btn.setOnAction(ev -> {

            bar.setProgress(bar.getProgress() + 0.1d);

            WritableImage image = new WritableImage(ICONSIZE, ICONSIZE);

            bar.snapshot(new SnapshotParameters(), image);

            File file;
            try {
                file = File.createTempFile("temp-image", ".png");
            } catch (IOException e1) {
                e1.printStackTrace();
                return;
            }

            try {
                ImageIO.write(SwingFXUtils.fromFXImage(image, null), "png", file);
            } catch (IOException e) {
                e.printStackTrace();
            }

            InputStream is;
            try {
                is = new FileInputStream(file);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                return;
            }

            primaryStage.getIcons().clear();
            primaryStage.getIcons().add(new Image(is));

            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        });

        VBox box = new VBox(bar, btn);
        primaryStage.setScene(new Scene(box, 300, 250));
        primaryStage.show();

    }
}
guleryuz
  • 2,714
  • 1
  • 15
  • 19
  • wow this is amazing, but, you're not using the windows property for sure, you're setting a progress bar as tray icon, this was really really amazing! – Yago Rodriguez Aug 28 '16 at 17:14
  • @YagoRodriguez No, he is taking a `snapshot` from the ProgressBar and save it to a file `image` and read that file as image and set it as the icon of the application, if you note, this will change the titlebar icon as well, he uses `+ 0.1d` on every click to increase the progress, at least as I undestand it. – TiyebM Jan 19 '19 at 23:25
2

This open source library called FXTaskbarProgressBar can help you.

If you want to show a progress at the taskbar:

//create the TaskbarProgressbar object
TaskbarProgressbar taskProgressbar = new TaskbarProgressbar(stage); // you must specify the stage in the constructor
//then you have to show the stage
stage.show();
//now you can display a progress
taskProgressbar.showOtherProgress(50, 100, TaskbarProgressbar.TaskbarProgressbarType.NORMAL);

Hope this helps :). For more information just go to the repostitory.