简体   繁体   English

如何在Java Swing中关闭PrinterJob对话框?

[英]How to close a PrinterJob dialog in Java Swing?

I'm having the following issue: 我遇到以下问题:

Our Swing application has a timeout which returns the user back to the login frame. 我们的Swing应用程序具有超时,该超时会使用户返回登录框架。

If a user opens a print dialog and leaves it open, and then tries to hit the print button after the timeout, our applications fails and the user is presented with an error. 如果用户打开一个打印对话框并将其保持打开状态,然后在超时后尝试单击打印按钮,则我们的应用程序将失败,并向用户显示一个错误。 (The application logic expects the user to make a print choice and then executes some logic afterwards). (应用程序逻辑希望用户做出打印选择,然后再执行一些逻辑)。

Is it possible to somehow close the print dialog when the application times out, or add a custom timeout to the print dialog? 当应用程序超时时,是否可以以某种方式关闭打印对话框,或者向打印对话框添加自定义超时?

EDIT: What i have tried so far (MCVE): 编辑:到目前为止,我已经尝试过(MCVE):

public class JavaPrintTest implements Printable {
    public int print(Graphics g, PageFormat pf, int pageIndex) {
        if (pageIndex != 0)
            return NO_SUCH_PAGE;
        Graphics2D g2 = (Graphics2D) g;
        g2.setFont(new Font("Serif", Font.PLAIN, 36));
        g2.setPaint(Color.black);
        g2.drawString("Java Source and Support!", 144, 144);
        return PAGE_EXISTS;
    }

    public static void main(String[] args) {

        new java.util.Timer().schedule(
                new java.util.TimerTask() {
                    @Override
                    public void run() {
                        Window[] allWindows = Window.getWindows();
                        for (Window w:allWindows) {
                            w.setVisible(false);
                            w.dispose();
                        }
                        for (Frame frame : Frame.getFrames() ) {
                            frame.setVisible(false);
                            frame.dispose();
                        }
                    }
                },
                5000
        );

        PrinterJob pj = PrinterJob.getPrinterJob();
        pj.setPrintable(new JavaPrintTest());

        if (pj.printDialog()) {
            try {
                pj.print();
            } catch (PrinterException e) {
                System.out.println(e);
            }
        }
    }
}

I start a timer that after 5 seconds will try to close the print dialog where it tries to iterate over all windows and all frames, but the dialog dosn't close. 我启动一个计时器,该计时器将在5秒钟后尝试关闭打印对话框,在该对话框中尝试遍历所有窗口和所有框架,但该对话框没有关闭。

You can find & close all active windows before returning to the log-in window: 您可以找到并关闭所有活动窗口,然后再返回登录窗口:

private void logout() {
    Window[] allWindows = Window.getWindows();
    for (Window w:allWindows) {
        w.setVisible(false);
        w.dispose();
    }
    returnToLoginWindow();
}

EDIT: Print dialog is a native system dialog window which is not affected by dispose(). 编辑:“打印”对话框是本机系统对话框窗口,不受dispose()的影响。
On Windows, for example, Java returns sun.awt.windows.WPrintDialog . 例如,在Windows上,Java返回sun.awt.windows.WPrintDialog
For such native dialogs, the only way I found is as a workaround: 对于此类本机对话框,我发现的唯一方法是解决方法:

for (Window w: allWindows) {
    //System.out.println(w.getClass().getSimpleName()+": "+w);
    try {
        Robot r = new Robot();
        r.keyPress(KeyEvent.VK_ESCAPE);
        r.keyRelease(KeyEvent.VK_ESCAPE);
    } catch (AWTException e) {
        e.printStackTrace();
    }
}

The following API does not affect the native dialog (at least on Windows): 以下API 不会影响本机对话框(至少在Windows上如此):

WindowEvent windowClosing = new WindowEvent(w, WindowEvent.WINDOW_CLOSING);
Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(windowClosing);
w.dispatchEvent(windowClosing);
w.setVisible(false);
w.dispose();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM