Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print contents of JavaFx TableView

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

like image 778
bluchip.gr Avatar asked May 24 '13 15:05

bluchip.gr


Video Answer


3 Answers

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();
like image 167
Asimaruk Avatar answered Sep 19 '22 05:09

Asimaruk


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();
                    }
                }
            }
        }
like image 26
Shantanu Chandra Avatar answered Sep 19 '22 05:09

Shantanu Chandra


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);
like image 20
windyDay Avatar answered Sep 22 '22 05:09

windyDay