Automatic Selection of Printer not working!

According to the instructions that I got from experts exchange earlier, I tried to develop a class file for sending a print option to a particular printer without prompting for selection of printer.  But when I try to press PRINT button an exception is coming and the print out is not appearing.  Please go through the program and suggest a better solution.  My aim is : There are 2-3 printers attached to a particular PC and a particular report print should go to a particular printer without a print dialog box.  

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.print.*;
import javax.swing.JEditorPane;
import javax.swing.RepaintManager;
import javax.swing.text.html.*;
import java.text.*;
import java.util.Date;
import java.util.Calendar;
import java.util.Iterator;
import javax.imageio.*;
import javax.print.*;
import javax.print.attribute.HashDocAttributeSet;
import javax.print.attribute.HashPrintRequestAttributeSet;
import java.awt.image.BufferedImage;
import java.awt.print.*;

public class TestReport extends JFrame implements ActionListener
 JPrintableEditPane jtp;
 Container content;
  Date d1,date;
 Calendar calendar;
 int day,month,year;
 JButton printButton, exit;
 String str, str1;
 public TestReport()
      day  = calendar.get(Calendar.DATE) ;
      month= calendar.get(Calendar.MONTH)+1 ;
      year = calendar.get(Calendar.YEAR);
      String todate=year+"-"+month+"-"+day;
str=str+"<HTML><HEAD></HEAD><BODY bgColor=white>";
str=str+"<p><font face=Verdana style=\"font-size: 10pt\">Suresh Babu V. S </font></b>";
str=str+"<p><font face=Verdana style=\"font-size: 10pt\">Trivandrum, India</font></b>";
      FileWriter f1=new FileWriter("c:\\IO.html");      //Open IO.html file for writing
      for (int i=0;i<str.length();++i)
           f1.write(str.charAt(i));                  f1.close();
catch(Exception e)
{System.out.println("Error in writing file"); }
   content = getContentPane();
   printButton = new JButton("Print");
   exit = new JButton ("Close");
   JPanel buttonPanel = new JPanel();
   content.add(buttonPanel, BorderLayout.SOUTH);
   jtp = new JPrintableEditPane();
   jtp.setEditorKit(new HTMLEditorKit());

    JScrollPane editorScrollPane = new JScrollPane(jtp);
      editorScrollPane.setPreferredSize(new Dimension(250, 145));
content.add(editorScrollPane, BorderLayout.CENTER);    
catch(IOException e){
     System.out.println("Print Error : "+e);

 public void actionPerformed(ActionEvent event) {
   if (event.getSource()==printButton)
DocFlavor flavor = null;//or the flavor you need
HashPrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
 PrintService printServices[] =
      PrintServiceLookup.lookupPrintServices(flavor, null);
   for (int i=0;i<4;i++)
DocPrintJob job = printServices[1].createPrintJob();
Doc doc = new SimpleDoc(jtp, flavor, new HashDocAttributeSet());
try {
             job.print(doc, pras);
      } catch (PrintException ex) {
public static void main(String args[])
      TestReport tr=new TestReport();

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

rupasurajAuthor Commented:
Any experts please go through my query and suggest a solution...

try this actionPerformed :)

public void actionPerformed(ActionEvent event) {
        if (event.getSource()==printButton)
            DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE;
            PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
            pras.add(new Copies(1));
            pras.add(new JobName("My job", null));
            PrintService printServices[] =
            PrintServiceLookup.lookupPrintServices(flavor, pras);
            for (int i=0;i<4;i++)
            DocPrintJob job = printServices[1].createPrintJob();
            Doc doc = new SimpleDoc(jtp, flavor,null);
            try {
                 job.print(doc, pras);
            } catch (PrintException ex) {

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sorry, please replace the code

DocPrintJob job = printServices[1].createPrintJob();

with this code

DocPrintJob job = printServices[0].createPrintJob();

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.