【问题标题】:SWT - AWT BridgeSWT - AWT 桥
【发布时间】:2012-11-07 15:38:06
【问题描述】:

主 GUI 是基于 SWT 的。我正在通过单击按钮从 printPDF 类运行打印操作。

 public void startPDFPrint() throws Exception {
   Display.getCurrent().syncExec(
        new Runnable() {
          public void run(){
             try {
              new AplotPdfPrintLocal().printPDF("c:\\temp\\file.pdf", "PDF Print Job");
           }
           catch (IOException e) {
              e.printStackTrace();
           }
           catch (PrinterException e) {
              e.printStackTrace();
           }
          }
        });
 }

printPDF 类没有任何组件或 GUI。它基本上只是创建一个运行打印作业。

public class PDFPrintPage implements Printable {

类中仅有的两个方法

 public void printFile(String filename) throws IOException { (setups the print)

  public int print(Graphics g, PageFormat format, int index)
        throws PrinterException {

在 printFile 方法中有一行代码可以打开本地打印机对话框

 pjob.printDialog()

对话框基于 AWT。

如何打开此对话框,以便我的用户可以选择打印机和份数?

我已阅读 SWT_AWT 桥接文档,看起来您需要将 AWT 嵌入 SWT 组件中,但我的类没有任何组件。

是否需要创建组件方法并在组件中运行 printFile 代码?

我知道如果我能弄清楚这篇文章,它也会帮助我解决我遇到的所有其他问题。

编辑

请查看我的代码并告诉我哪里出错了。它符合并运行,但我在 Dialog 行收到 SWT Thread 异常。

 public class PDFPrintPage extends ApplicationWindow{

  private String fileURL;
  private PageFormat pfDefault;
  private PrinterJob pjob;
  private PDFFile pdfFile;

  public PDFPrintPage(Shell parent, String inputFileName) {
     super(parent);
     this.fileURL = inputFileName;
  }

  public void run() {
    setBlockOnOpen(true);
    open();
    Display.getCurrent().dispose();
  }

  protected Control createContents(Composite parent) {
     final Composite swtAwtComponent = new Composite(parent, SWT.EMBEDDED);
     final java.awt.Frame frame = SWT_AWT.new_Frame( swtAwtComponent );
     final javax.swing.JPanel panel = new javax.swing.JPanel( );
     frame.add(panel);
     JButton swingButton = new JButton("Print");
     panel.add(swingButton);
     swingButton.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent actionevent) {
           try {
              printFile(fileURL, frame);
           }
          catch (IOException e) {
             e.printStackTrace();
          }
        }
     });
     return swtAwtComponent;
  }

  public void printFile(String filename, Frame panel) throws IOException {
     File file = new File(filename);
     FileInputStream fis = new FileInputStream(file);
     FileChannel fc = fis.getChannel();
     ByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
     pdfFile = new PDFFile(bb); // Create PDF Print Page

     final PrintPage pages = new PrintPage(pdfFile);

     pjob = PrinterJob.getPrinterJob();
     pfDefault = PrinterJob.getPrinterJob().defaultPage();
     Paper defaultPaper = new Paper();
     defaultPaper.setImageableArea(0, 0, defaultPaper.getWidth(),      defaultPaper.getHeight());
     pfDefault.setPaper(defaultPaper);
     pjob.setJobName(file.getName());

     final Dialog awtDialog = new Dialog(panel);      
     Shell parent = getParentShell();
     Shell shell = new Shell(parent, SWT.APPLICATION_MODAL | SWT.NO_TRIM);
     shell.setSize(100, 100);
     shell.addFocusListener(new FocusAdapter() {
        @Override 
        public void focusGained(FocusEvent e) {
           awtDialog.requestFocus();
           awtDialog.toFront();
        }
     });
     //if (pjob.printDialog()) {
        pfDefault = pjob.validatePage(pfDefault);
        Book book = new Book();
        book.append(pages, pfDefault, pdfFile.getNumPages());
        pjob.setPageable(book);
        try {
           pjob.print();
        }
        catch (PrinterException exc) {
           System.out.println(exc);
        }
     //}
  }

  class PrintPage implements Printable {

     private PDFFile file;

     PrintPage(PDFFile file) {
        this.file = file;
     }

     public int print(Graphics g, PageFormat format, int index) throws PrinterException {
       int pagenum = index + 1;
       if ((pagenum >= 1) && (pagenum <= file.getNumPages())) {
          Graphics2D g2 = (Graphics2D) g;
          PDFPage page = file.getPage(pagenum);
          Rectangle imageArea = new Rectangle((int) format.getImageableX(), (int) format.getImageableY(),
              (int) format.getImageableWidth(), (int) format.getImageableHeight());
          g2.translate(0, 0);
          PDFRenderer pgs = new PDFRenderer(page, g2, imageArea, null, null);
          try {
             page.waitForFinish();
             pgs.run();
          } catch (InterruptedException ie) {

          }
          return PAGE_EXISTS;
        } 
        else {
          return NO_SUCH_PAGE;
        }
    } 
   }//End PrintPage Class
  }//End PDFPrintPage Class

我可能将您的建议代码添加到完全错误的位置。我的想法是在 focusGained(FocusEvent e) 方法中添加 printDialog 调用的位置。

【问题讨论】:

    标签: java swt awt


    【解决方案1】:

    由于我没有将其添加为评论的声誉,因此我将发布答案。对于偶然发现此问题并寻找“Control 类型中的方法 addFocusListener(FocusListener) 不适用于参数 (new FocusAdapter(){})”的任何其他人。

    问题是您有错误的导入。您可能已经导入了 awt FocusAdapter 而不是 swt,反之亦然。

    【讨论】:

      【解决方案2】:

      当您打开打印机对话框时,您需要打开一个大小为零的外壳,这样您的主 SWT 外壳看起来是不活动的,而您的 Swing 模态对话框在它上面。同样,您需要在关闭摆动对话框时关闭零大小的 Shell。

       java.awt.Dialog awtDialog = ...        
            Shell shell = new Shell(parent, SWT.APPLICATION_MODAL | SWT.NO_TRIM);
            shell.setSize(0, 0);
            shell.addFocusListener(new FocusAdapter() {
                public void focusGained(FocusEvent e) {
                    awtDialog.requestFocus();
                    awtDialog.toFront();
                }
            });
      

      参考: http://www.eclipse.org/articles/article.php?file=Article-Swing-SWT-Integration/index.html#sec-event-threads

      【讨论】:

      • 我收到以下错误 - Control 类型中的方法 addFocusListener(FocusListener) 不适用于参数 (new FocusAdapter(){}) -
      • 你能检查我上面的编辑吗?请看看你能不能告诉我我的代码在哪里?我在 Dialog 行收到线程异常。
      猜你喜欢
      • 1970-01-01
      • 2020-06-10
      • 1970-01-01
      • 2017-10-25
      • 2014-08-06
      • 2012-01-27
      • 2010-12-17
      • 2013-09-10
      • 1970-01-01
      相关资源
      最近更新 更多