有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在FXML中打印所选窗格

我想打印fxml包含窗格,但不知道如何打印。 我尝试了两个按钮动作代码

first

void doPrint(Node printPane) {
 PrinterJob job = PrinterJob.createPrinterJob();
  if (job != null && job.showPageSetupDialog(printPane.getScene().getWindow())) {
                    job.printPage(printPane);
                    job.endJob();
                }
            }
        }


second

boolean doPrint(Node printPane) {
        PrinterJob job = PrinterJob.createPrinterJob();
        if (job == null) {
            return false;
        }
        if (!job.printPage(printPane)) {
            return false;
        }
        return job.endJob();
    }

共 (2) 个答案

  1. # 1 楼答案

    试试这个动作事件

    @FXML
    private void PrintAction(ActionEvent event) {
    
    
        Printer printer = Printer.getDefaultPrinter(); //get the default printer
    javafx.print.PageLayout pageLayout = printer.createPageLayout(Paper.NA_LETTER, PageOrientation.PORTRAIT, Printer.MarginType.DEFAULT); //create a pagelayout.  I used Paper.NA_LETTER for a standard 8.5 x 11 in page.
    PrinterJob job = PrinterJob.createPrinterJob();//create a printer job
    
    if(job.showPrintDialog(tab_doctor_list.getScene().getWindow()))// this is very useful it allows you to save the file as a pdf instead using all of your printer's paper. A dialog box pops up, allowing you to change the "name" option from your default printer to Adobe pdf. 
    {
        double pagePrintableWidth = pageLayout.getPrintableWidth(); //this should be 8.5 inches for this page layout.
        double pagePrintableHeight = pageLayout.getPrintableHeight();// this should be 11 inches for this page layout.
    
    
        tab_doctor_list.prefHeightProperty().bind(Bindings.size(tab_doctor_list.getItems()).multiply(35));// If your cells' rows are variable size you add the .multiply and play with the input value until your output is close to what you want. If your cells' rows are the same height, I think you can use .multiply(1). This changes the height of your tableView to show all rows in the table.
        tab_doctor_list.minHeightProperty().bind(tab_doctor_list.prefHeightProperty());//You can probably play with this to see if it's really needed.  Comment it out to find out.
        tab_doctor_list.maxHeightProperty().bind(tab_doctor_list.prefHeightProperty());//You can probably play with this to see if it' really needed.  Comment it out to find out.
    
        double scaleX = pagePrintableWidth / tab_doctor_list.getBoundsInParent().getWidth();//scaling down so that the printing width fits within the paper's width bound.
        double scaleY = scaleX; //scaling the height using the same scale as the width. This allows the writing and the images to maintain their scale, or not look skewed.
        double localScale = scaleX; //not really needed since everything is scaled down at the same ratio. scaleX is used thoughout the program to scale the print out.
    
        double numberOfPages = Math.ceil((tab_doctor_list.getPrefHeight() * localScale) / pagePrintableHeight);//used to figure out the number of pages that will be printed.
    
    
    }
    

    }

    使用窗格的fx:id,而不是tab\u doctor\u list

  2. # 2 楼答案

    这将打印您传递到其中的任何节点

    private void printImage(Node node) {
        PrinterJob job = PrinterJob.createPrinterJob();
        if (job != null) {
            boolean success = job.printPage(node);
            if (success) {
                System.out.println("PRINTING FINISHED");
                job.endJob();
            }
        }
    }