Like when you're downloading something from Chrome the taskbar icon shows like this:
i would like to show the progress of my program in the taskbar like that. how can you do this using javaFX...?
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();
}
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With