Java Reference
In-Depth Information
Solution
Utilize the JavaFX Print API, new to JavaFX 8, to print designated nodes and to con-
struct sophisticated print dialogs. The following class uses the new Print API to print
the TextArea component when a print button is selected.
public class Recipe02_11 extends Application {
@Override
public void start(Stage primaryStage) throws
Exception {
TextArea ta = new TextArea();
ta.setWrapText(true);
final Printer selectedPrinter
= Printer.getDefaultPrinter();
Button printButton = new Button("Print");
printButton.setOnAction((ActionEvent event) -> {
print(ta, selectedPrinter);
});
FlowPane pane = new FlowPane();
pane.getChildren().add(ta);
pane.getChildren().add(printButton);
primaryStage.setScene(new Scene(pane, 500, 300));
primaryStage.show();
}
public void print(final Node node, Printer printer) {
PrinterJob job = PrinterJob.createPrinterJob();
job.setPrinter(printer);
if (job != null) {
boolean success = job.printPage(node);
if (success) {
job.endJob();
}
Search WWH ::




Custom Search