2013-01-24 180 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"); 
}