Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with printing in Java

Status
Not open for further replies.

maynard444

Programmer
Jul 1, 2003
13
0
0
US
Hello all, I need a little help with this code. I am attempting to print an image file when a button is pressed. Here is what code I have now:
This is in the main method:

JButton print_button = new JButton("Print");
print_button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
PrintChart printtest = new PrintChart();
printtest.PrintChart2("filename and path.gif");
}
});

This is in the class PrintChart():

public void PrintChart2(String filename){

System.out.println("Pressed");

PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
pras.add(OrientationRequested.LANDSCAPE);
pras.add(MediaSizeName.NA_LEGAL);
DocFlavor flavor = DocFlavor.INPUT_STREAM.GIF;

PrintService printService[] = PrintServiceLookup.lookupPrintServices(flavor, pras);
PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService();

PrintService service = ServiceUI.printDialog(null, 200, 200, printService, defaultService, flavor, pras);
if (service != null)
{
DocPrintJob job = service.createPrintJob();
FileInputStream fis = new FileInputStream(filename);
DocAttributeSet das = new HashDocAttributeSet();
das.add(OrientationRequested.LANDSCAPE);
das.add(MediaSizeName.NA_LEGAL);
Doc doc = new SimpleDoc(fis, flavor, das);
job.print(doc, pras);
Thread.sleep(10000);
}
System.exit(0);
}
To be honest I don't know what is going on. When the button is pressed the method executes but nothing prints. I tested the printing portion in a seperate program to see if it was at fault but it was not. Any help would be appreciated. Thanks,
Heath
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top