Print contents of JavaFx TableView
Solution 1
Printing API appeared in fx8.0. And it can print nodes. You can create printer job with javafx.print.PrinterJob class. But it prints only region that fits to a printed page, and not the one that you on a screen. So you need to make your node fit page(scale, translate, etc) by hands. Here is simple printing example:
PrinterJob printerJob = PrinterJob.createPrinterJob();
if(printerJob.showPrintDialog(primaryStage.getOwner()) && printerJob.printPage(yourNode))
printerJob.endJob();
Solution 2
Snip the area you want
Rectangle rect = new Rectangle(0,0,dataDisplayAreaAnchorPane.getWidth(),dataDisplayAreaAnchorPane.getHeight());
dataDisplayAreaAnchorPane.setClip(rect);
WritableImage writableImage;
writableImage = new WritableImage((int) dataDisplayAreaAnchorPane.getPrefWidth(),
(int) dataDisplayAreaAnchorPane.getPrefHeight());
dataDisplayAreaAnchorPane.snapshot(null, writableImage);
eventDispatcher.printLandscape(writableImage);
**------------------------------------**
Resize according to A4 paper size and print
public void print(WritableImage writableImage, Stage primaryStage) {
ImageView imageView =new ImageView(writableImage);
Printer printer = Printer.getDefaultPrinter();
PageLayout pageLayout = printer.createPageLayout(Paper.A4, PageOrientation.LANDSCAPE, Printer.MarginType.DEFAULT);
double scaleX = pageLayout.getPrintableWidth() / imageView.getBoundsInParent().getWidth();
double scaleY = pageLayout.getPrintableHeight() / imageView.getBoundsInParent().getHeight();
imageView.getTransforms().add(new Scale(scaleX, scaleY));
PrinterJob job = PrinterJob.createPrinterJob();
if (job != null) {
boolean successPrintDialog = job.showPrintDialog(primaryStage.getOwner());
if(successPrintDialog){
boolean success = job.printPage(pageLayout,imageView);
if (success) {
job.endJob();
}
}
}
}
Solution 3
I have tried this . It first shows a page dialog window, then scales, translates and prints.
cmItem2.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent e){
Printer printer = Printer.getDefaultPrinter();
Stage dialogStage = new Stage(StageStyle.DECORATED);
PrinterJob job = PrinterJob.createPrinterJob(printer);
if (job != null) {
boolean showDialog = job.showPageSetupDialog(dialogStage);
if (showDialog) {
table.setScaleX(0.60);
table.setScaleY(0.60);
table.setTranslateX(-220);
table.setTranslateY(-70);
boolean success = job.printPage(table);
if (success) {
job.endJob();
}
table.setTranslateX(0);
table.setTranslateY(0);
table.setScaleX(1.0);
table.setScaleY(1.0);
}
}
}});
ContextMenu menu = new ContextMenu();
menu.getItems().addAll(cmItem1, cmItem2);
table.setContextMenu(menu);
![bluchip.gr](https://i.stack.imgur.com/aJhUa.png?s=256&g=1)
bluchip.gr
Updated on June 14, 2022Comments
-
bluchip.gr about 2 years
I am looking for a way to print the contents of a JavaFX TableView. I understand that JavaFX doesn't have Printing capabillities just yet (what a disapointment). I have found some information about taking a screenshot of a WebView for example and print it as an image.
Is it possible to do something like that with a Table view. How to go about to handle multiple pages on tables with many data.
Thanks for your help
-
Alexander Kirov about 11 yearsJavaFX 8 printing feature is available within JDK 8, and can be accessed via early access builds : jdk8.java.net/download.html. If you have any suggestions on functionality, you may file feature requests in javafx-2-jira.
-
bluchip.gr about 11 yearsHello, when you say "if I can't use Java 8" what are the reasons that I wont be able to use Java 8. I am building an application that will be sold to potential customers and will be deployed via web start. So my customers will have to have have JRE 8 is that right? So iguess my final question is can I use Java 8 for finalized market ready product considering that I will have to update it regularly? Thanks again for your help