Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Print dialog

 
jacob raja
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have add a menu item print in the File Menu and when i click the print menu item the print dialog should open as it is opening in MsWord i am using the following code for the dialog to pop up , but it does not pop up, if anyone know how to resolve it, it would be great helpfull to me.

The code that i used is
FileDialog fileChooser = new FileDialog(shell, SWT.OPEN);
String fileName = fileChooser.open();


if (fileName == null) { return; }

// Load the image
ImageLoader loader = new ImageLoader();
ImageData[] imageData = loader.load(fileName);
PrinterData printerData = null;
if (imageData.length > 0) {//
// Show the Choose Printer dialog
PrintDialog dialog = new PrintDialog(shell, SWT.CLOSE);
try{
printerData = dialog.open();

}catch(Exception e){
e.printStackTrace();
}

if (printerData != null) {
// Create the printer object
Printer printer = new Printer(printerData);

// Calculate the scale factor between the screen resolution and printer
// resolution in order to correctly size the image for the printer
Point screenDPI = display.getDPI();
Point printerDPI = printer.getDPI();
int scaleFactor = printerDPI.x / screenDPI.x;
// Determine the bounds of the entire area of the printer
Rectangle trim = printer.computeTrim(0, 0, 0, 0);
// Start the print job
if (printer.startJob(fileName)) {
if (printer.startPage()) {
GC gc = new GC(printer);
Image printerImage = new Image(printer, imageData[0]);

// Draw the image
gc.drawImage(printerImage, 0, 0, imageData[0].width,
imageData[0].height, -trim.x, -trim.y,
scaleFactor * imageData[0].width,
scaleFactor * imageData[0].height);

// Clean up
printerImage.dispose();
gc.dispose();
printer.endPage();
}
}
// End the job and dispose the printer
printer.endJob();
printer.dispose();
} else {
System.out.println("No data to print");
}

KINDLY HELP ME TO KNOW IF ANY THING THAT NEEDED TO BE CHANGED TO BRING THE PRINTER DIALOG BOX.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47232
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Complicated, that is. Get a copy of a book I quote often, C Horstmann and G Cornell, Core Java 2 (vol II advanced features), 7th Edition, Santa Clara CA, Sun Microsystems Press (Prentice-Hall), and read pages 539 to 575.
Once you get through their simpler examples it becomes a lot easier.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic