当我在EDT的线程上调用我的print方法时没有任何反应?

时间:2013-03-22 13:37:46

标签: java multithreading swing

我有一个method进行一些打印,我希望任务在另一个线程(不在EDT上)上运行,因为它可能正在创建一个大文件,我不希望长进程冻结GUI 。执行在EDT上完美运行(当然GUI冻结 - 这是不可取的),但是当在不同的线程上调用时,它就不会执行。
这是方法;

                    buildReceipt(itemList, method);

其中;

  • itemList是用于填充收据的ArrayList,
  • methodenum类型,用于确定是将输出设为.pdf文件还是直接将其发送到打印机

上面的代码在EDT上执行时很好地生成了文档,但当我尝试使用doInBackground() SwingWorker方法将其作为后台任务时,它根本没有做任何事情;然后我好奇并尝试了以下内容;

Thread thread = new Thread(new Runnable(){
    @Override
    public void run()
    {
        buildReceipt(itemList, method);
    }
});
thread.start();
然而,没有任何事情发生.........更加有趣和令人困惑的是,我甚至尝试过SwingUtilities.InvokeLater& SwingUtilities.InvokeAndWait(由文件在EDT上运行)但仍无济于事。

我尽可能多地搜索Stack Overflow相关问题,但没有一个能解决我的奇怪问题。我真的需要帮助。自yesteray以来一直被困???!?!



编辑:

参与Jean Waghetti;这里简要介绍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;
}

1 个答案:

答案 0 :(得分:1)

所以基本上你的项目列表是空的,因此它永远不会在该方法的IF条件中执行代码。