有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java SWT UI线程未响应打印错误

enter image description here

我的SWT标题中有一个打印按钮

viewPDFButton.addSelectionListener(new SelectionAdapter() {
     public void widgetSelected(SelectionEvent e) {
        try {

           startPdfPrintOperation();
        }
        catch (Exception e1) {
           // TODO Auto-generated catch block
           e1.printStackTrace();
        }
     }
  }); 

我从表中的用户选择中获取现有PDF文件名和路径。 然后我想把pdf文件打印到本地打印机上。需要允许用户选择所选的本地打印机

public void startPdfPrintOperation() throws Exception {
  File file = new File(getPDFFileName());
  RandomAccessFile raf;
  raf = new RandomAccessFile(file, "r");
  FileChannel channel = raf.getChannel();
  ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
  pdfFile = new PDFFile(buf);
  PDFPrintPage pages = new PDFPrintPage(pdfFile);

  // Create Print Job
  pjob = PrinterJob.getPrinterJob();
  pjob.setPrintable(new MyPrintable());
  final HashPrintRequestAttributeSet attset;
  attset = new HashPrintRequestAttributeSet ();
  attset.add (new PageRanges (1, pdfFile.getNumPages ()));
  if (pjob.printDialog (attset)) {
      try {
           pjob.print (attset);
      }
      catch (PrinterException e) {
           e.printStackTrace();
      }
  }             
}
class MyPrintable implements Printable {
  public int print (Graphics g, PageFormat format, int index) throws PrinterException {
     int pagenum = index+1;
        if (pagenum < 1 || pagenum > pdfFile.getNumPages ())
            return NO_SUCH_PAGE;

        Graphics2D g2d = (Graphics2D) g;
        AffineTransform at = g2d.getTransform ();
        PDFPage pdfPage = pdfFile.getPage (pagenum);

        Dimension dim;
        dim = pdfPage.getUnstretchedSize ((int) format.getImageableWidth (),
                                       (int) format.getImageableHeight (),
                                       pdfPage.getBBox ());

        Rectangle bounds = new Rectangle ((int) format.getImageableX (),
                                       (int) format.getImageableY (),
                                       dim.width,
                                       dim.height);

        PDFRenderer rend = new PDFRenderer (pdfPage, (Graphics2D) g, bounds, null, null);

        try
        {
           pdfPage.waitForFinish ();
           rend.run ();
        }
        catch (InterruptedException ie)
        {
           MessageDialog.openError(null, "PDF Error Message", "Needs");
        }
        g2d.setTransform (at);
        g2d.draw (new Rectangle2D.Double (format.getImageableX (),
                                       format.getImageableY (),
                                       format.getImageableWidth (),
                                       format.getImageableHeight ()));
     return PAGE_EXISTS;
  }
}

我从第315行得到上述错误

if (pjob.printDialog (attset)) {

打印机对话框打开时,整个应用程序将被冻结且无响应。然后在大约30秒内,我得到了上面的错误

我曾尝试在多个地点使用Display.getDefault().asyncExec(new Runnable() ),但都没有用

可能是因为基本对话框是SWT,而打印机对话框是AWT


共 (1) 个答案

  1. # 1 楼答案

    由于您没有定义“在多个点中”,我建议您在自己的类中重构打印作业,扩展Thread,并在run方法中实现启动打印作业
    我不熟悉上面代码中的所有类,您可以尝试简单地启动这个线程,它将与SWT线程并行运行。尝试避免共享资源,这可能有助于解决死锁。如果您想从该线程获得UI响应,可以包装SWT消息框(“打印完成!”)在对Display.getDefault().asyncExec(new Runnable() { ... }的调用中
    此外,请测试没有UI代码的打印代码是否会产生相同的异常。如果是这样,环境可能会被错误配置