我有一个方法
可以进行一些打印,我希望该任务在另一个线程上运行(而不是在EDT上),因为它可能会创建一个大文件,并且我不希望长时间的进程卡住GUI。该执行在 EDT 上完美运行(当然 GUI 卡住 - 这是不希望的),但是当在不同的线程上调用时,它只是不执行。
方法如下;
buildReceipt(itemList, method);
哪里;
itemList
是一个用于填充收据的 ArrayList,并且method
是一个enum
类型,用于确定是将输出设为 .pdf 文件还是将其直接发送到打印机
上面的代码在 EDT 上执行时可以很好地生成文档,但是当我尝试使用 SwingWorker
的 doInBackground()
方法将其设置为后台任务时,它根本没有执行任何操作;然后我很好奇并尝试了以下操作;
Thread thread = new Thread(new Runnable(){
@Override
public void run()
{
buildReceipt(itemList, method);
}
});
thread.start();
仍然没有发生任何事情......更有趣和令人困惑的是,我什至尝试了 SwingUtilities.InvokeLater
和 SwingUtilities.InvokeAndWait
(根据文档在 EDT 上运行)但仍然无济于事。
我已经搜索了尽可能多的 Stack Overflow 相关问题,但没有一个解决我的奇怪问题。我真的需要这方面的帮助。从昨天开始就卡住了?!?!?!
编辑:
回应让·瓦盖蒂;以下是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 条件中的代码。
关于java - 当我在 EDT 之外的线程上调用打印方法时没有任何反应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15571755/