在应用程序运行时刷新 Java 中的打印机 [英] Refresh printers in Java while application is running

查看:36
本文介绍了在应用程序运行时刷新 Java 中的打印机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所说,我想在我的 Java 应用程序运行时刷新在计算机设置中注册的打印机.通常,我可以使用 PrinterJob.lookupPrintServices() 来获取打印机.但是,这些仅在重新启动应用程序时刷新.我读过一些关于 lookupPrintServices() 应该在新线程中完成以获得打印机的内容.但是这不起作用,打印机列表保持不变.以下链接表明这个问题应该在 Java 5.0 中得到修复,是我做错了什么?

As the title says, I would like to refresh the printers that are registered in the settings of the computer while my Java application is running. Normally, I can use PrinterJob.lookupPrintServices() to get the printers. However, these are only refreshed when restarting the application. I've read something on that lookupPrintServices() should be done in a new thread in order to get the printers. This however did not work, the list of printers remains the same. The following link shows that this problem should have been fixed in Java 5.0, am I doing something wrong?

非常感谢任何帮助!

编辑添加了 MWE.

public class MTPrinterTest extends Thread {
    public static void main(String[] args) {
        MTPrinterTest t1 = new MTPrinterTest();
        t1.start();

        try {
            System.in.read();
        } catch (Exception e){}

        MTPrinterTest t2 = new MTPrinterTest();
        t2.start();
    }
    public void run() {
        PrinterJob printerJob;
        PrintService[] printServices;

        printerJob = PrinterJob.getPrinterJob();
        printServices = printerJob.lookupPrintServices();
        System.out.println("Number of servies found: " + printServices.length);
        for (int i =0; i< printServices.length; i++)
            System.out.println("--> Available Printer " + i + ": " + printServices[i]);
        printerJob.printDialog(); 
    } 
}

推荐答案

无需重新启动应用程序即可刷新打印服务列表.

在这里我找到了解决方案:

/**
 * Printer list does not necessarily refresh if you change the list of 
 * printers within the O/S; you can run this to refresh if necessary.
 */
public static void refreshSystemPrinterList() {
    Class<?>[] classes = PrintServiceLookup.class.getDeclaredClasses();
    for (Class<?> clazz : classes) {
        if ("javax.print.PrintServiceLookup$Services".equals(clazz.getName())) {
            // sun.awt.AppContext.getAppContext().remove(clazz);
            // Use reflection to avoid "Access restriction" error message
            try {
                Class<?> acClass = Class.forName("sun.awt.AppContext");
                Object appContext = acClass.getMethod("getAppContext").invoke(null);
                acClass.getMethod("remove", Object.class).invoke(appContext, clazz);
            } catch (Exception e) {
            }
            break;
        }
    }
}

基本上,静态类PrintServiceLookup.Services 维护打印服务列表.因此,如果您从 AppContext 中删除此类,则会强制 PrintServiceLookup 再次创建一个新实例.因此,打印服务列表得到刷新.

Basically, the static class PrintServiceLookup.Services maintains the list of print services. So, if you remove this class from the AppContext, you force PrintServiceLookup to create a new instance again. Thus, the list of print services gets refreshed.

这篇关于在应用程序运行时刷新 Java 中的打印机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆