Printing JavaFx TableView Content

I am looking for a way to print the contents of a JavaFX TableView. I understand that JavaFX does not yet have print capabilities (which is a disappointment). I found some information about a WebView screenshot, for example, and printed it as an image.

Is it possible to do something similar with a table view. How to proceed to processing multiple pages in tables with many data.

thanks for the help

+4
source share
4 answers

The print API appeared in fx8.0. And he can print nodes. You can create a printer job with the javafx.print.PrinterJob class. But it prints only the area that is suitable for the printed page, and not the one that you are on the screen. Therefore, you need to make your page node (scale, translation, etc.) with your hands. Here is a simple print example:

PrinterJob printerJob = PrinterJob.createPrinterJob(); if(printerJob.showPrintDialog(primaryStage.getOwner()) && printerJob.printPage(yourNode)) printerJob.endJob(); 
+6
source

Remove 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 to A4 paper 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(); } } } } 
+3
source

I have tried this. It first displays the page dialog box, 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); 
+1
source

I understand that JavaFX does not yet have printability (which is a disappointment).

Java 8 includes printing capabilities for JavaFX. https://jdk8.java.net/download.html

Java 8 print functions are implemented in RT-17383 Printing and functions as described in @Asimaruk's answer.

I found some information about a screenshot of a WebView, for example, and printed it as an image. Is it possible to do something similar with a table view.

This can be done if you cannot use Java 8.

Using JavaFX api snapshot, use SwingFXUtils to convert JavaFX image to BufferedImage, use Swing print methods to print BufferedImage.

How to proceed to processing multiple pages in tables with many data.

If you cannot use Java 8, use the snapshot method above for each page, scrolling the table data between each snapshot.


Also see RT-17665. Some user interface controls support printing of their content , which is a request for direct access api added to some controls (e.g. TableView), so you can edit tableView.print () to print only the table, not the entire scene. This handy printing API is not currently implemented, but you can vote or comment on a related issue to indicate your interest in this feature.

0
source

Source: https://habr.com/ru/post/1482591/


All Articles