Meaningless Drivel is fun!*
The moose likes Swing / AWT / SWT and the fly likes Print dialog Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Print dialog " Watch "Print dialog " New topic
Author

Print dialog

jacob raja
Greenhorn

Joined: Feb 20, 2007
Posts: 7
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.


Luv<br />jacob Marshall
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38483
    
  23
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.
 
Don't get me started about those stupid light bulbs.
 
subject: Print dialog