Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can you send information to the windows task bar from java o javafx

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...?

like image 950
Yago Rodriguez Avatar asked Aug 28 '16 14:08

Yago Rodriguez


2 Answers

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

    }
}
like image 69
guleryuz Avatar answered Nov 10 '22 07:11

guleryuz


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.

like image 44
DanyProgramer Avatar answered Nov 10 '22 07:11

DanyProgramer