如何在FXML中打印选定的窗格

时间:2017-10-31 09:21:38

标签: java javafx printing pane

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

      .un-progress {
        height: 10px !important; 
        margin-top: -0.325em;
        margin-left: -2.75em;
        margin-right: 1.5em;
      }
      .finished_time {
        margin-left: 6.25em;
        margin-top: -0.8em
      }
      .left_time {
        margin-left: 22.0em;
        margin-top: -2.5em;
      }

    @media (min-width: 480px) {
        .un-progress{
          // changes here
        }
        .finished_time{
          // changes here
        }
        .left_time{
          // changes here
        }
      }
    }

    @media (min-width: 768px) {
        .un-progress{
          // changes here
        }
        .finished_time{
          // changes here
        }
        .left_time{
          // changes here
        }
      }
    }

    @media (min-width: 1024px) {
        .un-progress{
          // changes here
        }
        .finished_time{
          // changes here
        }
        .left_time{
          // changes here
        }
       }
     }

2 个答案:

答案 0 :(得分:0)

这将打印您传递给它的任何节点。

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();
        }
    }
}

答案 1 :(得分:0)

在动作事件中尝试此操作

@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.


}

}

而不是 tab_doctor_list 使用您的 fx:id 窗格。

相关问题