Java Reference
In-Depth Information
14-17. Printing with JavaFX
Problem
You want to provide the ability to print a designated node in your application scene
graph.
Solution
Utilize the JavaFX Print API, new to JavaFX 8, to print designated nodes, and to con-
struct sophisticated print dialogs. In this solution, a JavaFX application for drawing is
generated. The drawing application allows you to print the canvas via a Print button.
When the Print button is invoked, a dialog is opened that provides printing options such
as printer and layout selection.
The following code is used to construct the application stage, including all buttons
and drawing features. This first class does not contain any of the printing logic—you'll
see that next—and these sources are being shown to make it easy to follow along with
the example.
public class PrintingWithJavaFX extends Application {
static Stage PRINT_DIALOG;
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Application.launch(PrintingWithJavaFX.class, args);
}
private static Stage createPrintDialog(Stage parent,
boolean modal, Canvas node) {
if (PRINT_DIALOG != null) {
PRINT_DIALOG.close();
}
// Copy canvas
Search WWH ::




Custom Search