Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8854987
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:01:02+00:00 2026-06-14T14:01:02+00:00

Main GUI is SWT based. I am running a print operation from a printPDF

  • 0

Main GUI is SWT based. I am running a print operation from a printPDF class with a button click.

 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();
           }
          }
        });
 }

The printPDF class is does not have any components or GUI. It just basically creates a run a print job.

public class PDFPrintPage implements Printable {

The only two methods in the class

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

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

In the printFile method there is a line of code that opens a local printer dialog

 pjob.printDialog()

The dialog is based in AWT.

How can I get this dialog to open, so my user can select a printer and number of copies?

I have read over the SWT_AWT bridge documentation, It looks like you need to embed AWT in a SWT Component, but my class does not have any components.

Do I need to create a component method and run the printFile code in the component?

I know if I can figure out this piece, it will also help with all my other issues I am having.

EDIT

Please look at my code and show me where I have it wrong. It complies and runs, but I am getting SWT Thread exception at the Dialog line.

 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

I may be adding your suggestion code in completely the wrong spot. My thoughts where to add the printDialog call in the focusGained(FocusEvent e) method.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T14:01:03+00:00Added an answer on June 14, 2026 at 2:01 pm

    You need to open a shell with zero size when you open your printer dialog so that it will look like your main SWT Shell is inactive and your Swing modal dialog on top it. Similarly you need to close the zero size Shell when you close your swing dialog.

     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();
              }
          });
    

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Main Activity: private Button btnSubmit; private DataSource mDataSource; private Context mContext; @Override public void
I have main class with a main GUI from where I want to activate
I have a class library that is nested two+ layers under a main GUI
I'm wondering if SwingWorker has to be a nested class within my main GUI.
I have an application which runs 2 worker threads separate from the main GUI
I have my main GUI thread, and a second thread running inside it's own
I wrote little WPF application with 2 threads - main thread is GUI thread
I have 2 Classes. GUI (with main method in it) Parser (this reads files(csv),
I have a GUI application whose main part is a QPlainTextEdit . It is
I have created a very simple GUI project in Qt as follows: main: #include

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.