简体   繁体   English

当我在EDT以外的线程上调用我的打印方法时,什么都没发生?

[英]Nothing happens when I call my print method on a thread off the EDT?

I have a method that does some printing and I want the task to run on another thread (not on the EDT) because it might be creating a large file and I dont want the long process to freeze the GUI. 我有一个method可以进行一些打印,我希望任务在另一个线程上运行(而不是在EDT上),因为它可能正在创建一个大文件,并且我不希望漫长的过程冻结GUI。 The execution works perfectly on the EDT (with GUI freezing of course - which isn't desired), but when invoked on a different thread, it just doesn't execute. 该执行在EDT上运行完美(当然不冻结GUI,这是不希望的),但是在其他线程上调用时,它不会执行。
Here goes the method; 方法在这里;

                    buildReceipt(itemList, method);

Where; 哪里;

  • itemList is an ArrayList used to populate the receipt, and itemList是一个ArrayList,用于填充收据,并且
  • method is an enum Type that determines whether to make the output a .pdf File or Send it directly to a Printer method是一个enum类型,它确定是输出.pdf文件还是直接将其发送到打印机

The code above produces the document nicely when executed on the EDT but when I tried making it a background task using doInBackground() method of SwingWorker , it just didn't do anything at all; 上面的代码在EDT上执行时可以很好地生成文档,但是当我尝试使用SwingWorker doInBackground()方法使其成为后台任务时,它什么也没做。 Then I got curious and tried the following; 然后我很好奇并尝试了以下方法;

Thread thread = new Thread(new Runnable(){
    @Override
    public void run()
    {
        buildReceipt(itemList, method);
    }
});
thread.start();


and still, nothing happened......... More funny and confusing is the fact that I have even tried SwingUtilities.InvokeLater & SwingUtilities.InvokeAndWait (which by documentation run on the EDT) but still to no avail. 仍然没有任何事情发生……......更有趣和令人困惑的是,我什至尝试了SwingUtilities.InvokeLaterSwingUtilities.InvokeAndWait (根据EDT运行的文档),但仍然无济于事。

I have searched as many Stack Overflow related questions as I could, but none addresses my strange problem. 我已经搜索了尽可能多的与堆栈溢出相关的问题,但是没有一个解决我的奇怪问题。 I really need help on this one. 我真的需要这方面的帮助。 Been stuck since yesteray?!?!?! 自从yesteray以来一直被卡住?!?!?!



EDIT: 编辑:

In Respose to Jean Waghetti; 寄给让·瓦格蒂(Jean Waghetti); here's briefly what happens inside buildReceipt 这是buildReceipt内部发生的简要情况

 private boolean buildReceipt(ArrayList<Sales> itemList, PrintMethod method) { boolean built = false; if(!itemList.isEmpty()) { InvoiceDesign design = new InvoiceDesign(itemList); try { JasperReportBuilder report = design.build(); if(method.equals(PrintMethod.PDF)) { appManager.connectToDB(); File fileDir = appManager.getReceiptsDir(); appManager.disconnectDB(); FileOutputStream fos = new FileOutputStream(fileDir); report.toPdf(fos); fos.close(); built = true; } else if(method.equals(PrintMethod.PRINTER)) { report.print(true); built = true; } } catch(IOException e) { e.printStackTrace(); } catch (DRException e) { e.printStackTrace(); } } return built; } 

因此,基本上您的项目列表为空,因此它永远不会在该方法的IF条件下执行代码。

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

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