2013-01-24 83 views
1

可以使用Java Print Service API獲取默認系統打印機嗎?Java。獲取系統默認打印機

我可以用

PrintServiceLookup.lookupPrintServices(null, null) 

得到所有的打印機列表中,但如何讓打印機選用如默認系統? (在下面的屏幕截圖中,檢查了默認打印機(HP Laser Jet))。

choosed default printer

回答

9

您應該使用PrintServiceLookup

import javax.print.PrintServiceLookup; 
PrintService service = PrintServiceLookup.lookupDefaultPrintService(); 

命中率。到的Javadoc:

lookupDefaultPrintService位於此環境的默認打印服務。這可能會返回null。如果多個查找服務都指定默認值,則所選服務的定義不準確,但平臺本機服務(而非已安裝的服務)通常會作爲默認值返回。如果沒有清晰可識別的平臺本地默認打印服務,則默認情況下,首先將以實現相關的方式進行定位。

1
PrintService service = 
       PrintServiceLookup.lookupDefaultPrintService(); 
+0

即:'getDefaultPrintService()'不應該被appplications直接調用。 – Azodious

1

你可以使用PrintServiceLookup.lookupDefaultPrintService

PrintService service = 
    PrintServiceLookup.lookupDefaultPrintService(); 
if (service != null) { 
    String printServiceName = service.getName(); 
    System.out.println("Print Service Name is " + printServiceName); 
} else { 
    System.out.println("No default print service found"); 
}