maynard444
Programmer
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
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