This repository was archived by the owner on Aug 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
Process listening
Dansoftowner edited this page Feb 15, 2020
·
4 revisions
Sometimes, when you read a larger pdf document (maybe from a URL), it takes a little bit more time to read it than usuall.
That's why this library offers a tool called ProcessListener to show something to the user while the reading of the
pdf document is processing.
This tool called ProcessListener is a functional interface. It can take one boolean parameter which is true if the
process is finished or false if the process is not finished yet.
You can set the ProcessListener of the PDFDisplayer with the setProcessListener method.
PDFDisplayer pDisplayer = new PDFDisplayer();
pdDisplayer.setProcessListener(finished -> {
if (finished){
//your code here
} else {
//your code here
}
});If you want to execute javaFX tasks in the ProcessListener you have to use the Platform.runLater() method to
be sure that you are execute that tasks on the javaFX Application Thread.
pdDisplayer.setProcessListener(finished -> {
Platform.runLater(() -> {
if (finished) {
//your code here
} else {
//your code here
}
});
});import com.dansoftware.pdfdisplayer.PDFDisplayer;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ProgressBar;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import java.io.IOException;
import java.net.URL;
public class ProcessListenerDemo extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
//create a progressBar
ProgressBar progressBar = new ProgressBar();
progressBar.setPrefWidth(500);
progressBar.setProgress(0);
//create a pdfDisplayer
PDFDisplayer pdfDisplayer = new PDFDisplayer();
//set the process listener of it
pdfDisplayer.setProcessListener(finished ->
Platform.runLater(() -> {
if (finished) {
//when the process finished
progressBar.setProgress(0);
} else {
//when the process started
progressBar.setProgress(Timeline.INDEFINITE);
}
})
);
//create a btn for loading the pdf
Button loaderBtn = new Button("Load");
loaderBtn.setOnAction(e ->
//start a new thread for load the pdf document
new Thread(() -> {
try {
pdfDisplayer.displayPdf(new URL("https://www.tutorialspoint.com/javafx/javafx_tutorial.pdf"));
} catch (IOException ex) {
ex.printStackTrace();
}
}).start()
);
primaryStage.setScene(new Scene(new VBox(new StackPane(loaderBtn), pdfDisplayer.toNode(), new StackPane(progressBar))));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}Result:
