Skip to content

Instantly share code, notes, and snippets.

@afinlay5
Created March 3, 2020 18:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save afinlay5/23b570415a99c862d01775bfb4d0536c to your computer and use it in GitHub Desktop.
Save afinlay5/23b570415a99c862d01775bfb4d0536c to your computer and use it in GitHub Desktop.
Bug in Java Client code when calling AWT code from a JavaFX context
import java.io.*;
import java.net.URL;
import java.nio.file.*;
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.HBox;
import javafx.scene.control.Button;
public class ClientBug extends Application {
public static void main (String[] args) {
launch(args);
// loadDocAWT();
// System.out.println("once");
// loadDocAWT();
// System.out.println("twice");
// loadDocAWT();
// System.out.println("thrice");
}
@Override
public void start(Stage stage) {
Button btn = new Button("Click Me");
btn.setOnAction( ae -> loadDocAWT() );
HBox root = new HBox(btn);
Scene scene = new Scene(root, 400, 400);
stage.setScene(scene);
stage.show();
}
private static void loadDocAWT() {
try {
// File pdf = File.createTempFile("sample", ".pdf");
File pdf = new File ("sample.pdf");
InputStream in = new URL("https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf").openStream();
Files.copy(in, pdf.toPath(), StandardCopyOption.REPLACE_EXISTING);
System.out.println("before execution");
java.awt.Desktop.getDesktop().open(pdf);
System.out.println("successfully executed");
} catch (IOException ioe) {
//log
System.err.println("An I/O Error occurred in loading PDF data.");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment