我有一个method
进行一些打印,我希望任务在另一个线程(不在EDT上)上运行,因为它可能正在创建一个大文件,我不希望长进程冻结GUI 。执行在EDT上完美运行(当然GUI冻结 - 这是不可取的),但是当在不同的线程上调用时,它就不会执行。
这是方法;
buildReceipt(itemList, method);
其中;
itemList
是用于填充收据的ArrayList,method
是enum
类型,用于确定是将输出设为.pdf文件还是直接将其发送到打印机上面的代码在EDT上执行时很好地生成了文档,但当我尝试使用doInBackground()
SwingWorker
方法将其作为后台任务时,它根本没有做任何事情;然后我好奇并尝试了以下内容;
Thread thread = new Thread(new Runnable(){
@Override
public void run()
{
buildReceipt(itemList, method);
}
});
thread.start();
然而,没有任何事情发生.........更加有趣和令人困惑的是,我甚至尝试过SwingUtilities.InvokeLater
& SwingUtilities.InvokeAndWait
(由文件在EDT上运行)但仍无济于事。
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;
}
答案 0 :(得分:1)
所以基本上你的项目列表是空的,因此它永远不会在该方法的IF条件中执行代码。