2016-02-04 95 views
0

我在一個應用程序上工作,這個應用程序是在javafx中,在這個應用程序中我們正在接受食品訂單,這個訂單我們必須用不同的打印機打印,有些打印機會在總部的廚房裏。在我的系統中,我需要打印機列表,當我從應用程序中按下打印按鈕時,我將從列表中選擇打印機。所以打印作業將傳遞給選定的打印機。我將如何在我的javafx應用程序中完成此操作?如何將打印作業傳遞給javafx applcation中的特定打印機?

現在用這個下面的方法,但它傳遞的PrintJob默認它是由系統選擇的不是應用程序了打印機: -

public void print(Node node) { 
    Printer printer = Printer.getDefaultPrinter(); 
    PageLayout pageLayout = printer.createPageLayout(Paper.NA_LETTER, PageOrientation.PORTRAIT, Printer.MarginType.DEFAULT); 
    double scaleX = node.getBoundsInParent().getWidth(); 
    double scaleY = node.getBoundsInParent().getHeight(); 
    node.getTransforms().add(new Scale(scaleX, scaleY)); 

    PrinterJob job = PrinterJob.createPrinterJob(); 
    if (job != null) { 
     boolean success = job.printPage(node); 
     if (success) { 
      job.endJob(); 
     } 
    } 
} 

這是怎麼想過去打印機打印工作,而不是從得到print打印機:

ChoiceDialog dialog = new ChoiceDialog(Printer.getDefaultPrinter(), Printer.getAllPrinters()); 
    //ChoiceDialog dialog = new ChoiceDialog(printerName1, printerName2, printerName3, printerName4, printerName5); 
      dialog.setHeaderText("Choose the printer!"); 
      dialog.setContentText("Choose a printer from available printers"); 
      dialog.setTitle("Printer Choice"); 
      Optional<Printer> opt = dialog.showAndWait(); 
      if (opt.isPresent()) { 
       Printer printer = opt.get(); 
       PrinterJob job = PrinterJob.createPrinterJob(); 
       job.setPrinter(printer); 
       if (job != null) { 
        boolean success = job.printPage(node); 
        if (success) { 
         job.endJob(); 
        } 
       } 
      } 

回答

0

您可以使用ChoiceDialog爲宗旨,以從Printer.getAllPrinters返回打印機的Set選擇Printer

ChoiceDialog dialog = new ChoiceDialog(Printer.getDefaultPrinter(), Printer.getAllPrinters()); 
dialog.setHeaderText("Choose the printer!"); 
dialog.setContentText("Choose a printer from available printers"); 
dialog.setTitle("Printer Choice"); 
Optional<Printer> opt = dialog.showAndWait(); 
if (opt.isPresent()) { 
    Printer printer = opt.get(); 
    // start printing ... 
} 

當然,如果您不想使用對話框,也可以使用任何其他方式從項目列表中選擇一個項目。例如。

  • ListView
  • ComboBox
  • TableView

順便說一句:節點的大小將是0,除非他們layouted,這可能導致

double scaleX = node.getBoundsInParent().getWidth(); 
double scaleY = node.getBoundsInParent().getHeight(); 
node.getTransforms().add(new Scale(scaleX, scaleY)); 

它擴展到0。對於尚未顯示的節點,你需要自己的佈局(請參閱這樣的回答:https://stackoverflow.com/a/26152904/2991525):

Group g = new Group(node); 
Scene scene = new Scene(g); 
g.applyCss(); 
g.layout(); 
double scaleX = node.getBoundsInParent().getWidth(); 
double scaleY = node.getBoundsInParent().getHeight(); 

但我不知道你想反正與比例要達到什麼...越大,節點越大,縮放因子就越不合理,特別是如果高度和寬度不同。

+0

你好,謝謝你的幫助......我如何獲得更多的一份印刷品? ...我可以給打印機定製名稱嗎? – gurjeet

+0

@gurjeet:這些是2個新問題,或者是a)已經發布在本網站上(儘管還沒有在網站上搜索過),或b)可能對其他用戶有用。所以我不會在評論中回答他們。順便說一句:有時一個簡單的網絡搜索幫助。 – fabian

+0

這是我如何將打印作業傳遞給打印機,但沒有從打印機獲取打印: – gurjeet

相關問題