如何从java o javafx向windows任务栏发送信息

问题描述:

就像您从Chrome下载内容时一样,任务栏图标显示如下:

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

我想在任务栏中显示我的程序的进度。你怎么能用javaFX做这个...?

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

希望这种方式有所帮助

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();

    }
}