Skip to content

Instantly share code, notes, and snippets.

@twyatt
Last active January 3, 2016 20:58
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 twyatt/8518116 to your computer and use it in GitHub Desktop.
Save twyatt/8518116 to your computer and use it in GitHub Desktop.
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.print.PrinterJob;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
public class PrintTest extends Application {
private Pane root;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(final Stage primaryStage) throws Exception {
root = new Pane();
root.setOnMouseClicked(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent e) {
PrinterJob printerJob = PrinterJob.createPrinterJob();
System.out.println("Printing...");
if (printerJob.showPrintDialog(primaryStage.getOwner()) && printerJob.printPage(root)) {
System.out.println("Print successful."); // <-- this line is never reached on OS X
printerJob.endJob();
}
}
});
Rectangle r = new Rectangle();
r.setX(50);
r.setY(50);
r.setWidth(200);
r.setHeight(100);
r.setArcWidth(20);
r.setArcHeight(20);
root.getChildren().add(r);
Scene scene = new Scene(root, 800, 600);
primaryStage.setScene(scene);
primaryStage.show();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment