rupasuraj
asked on
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.
/************************* ********** */
// TestReport.java
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.print.*;
import java.io.Serializable;
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.io.*;
import java.util.Iterator;
import javax.imageio.*;
import javax.imageio.stream.Image InputStrea m;
import javax.print.*;
import javax.print.attribute.Hash DocAttribu teSet;
import javax.print.attribute.Hash PrintReque stAttribut eSet;
import java.awt.image.BufferedIma ge;
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()
{
setSize(800,550);
calendar=Calendar.getInsta nce();
day = calendar.get(Calendar.DATE ) ;
month= calendar.get(Calendar.MONT H)+1 ;
year = calendar.get(Calendar.YEAR );
String todate=year+"-"+month+"-"+ day;
str="";
str=str+"<HTML><HEAD></HEA D><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>";
str=str+"</body></html>";
try
{
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");
printButton.addActionListe ner(this);
exit.addActionListener(thi s);
JPanel buttonPanel = new JPanel();
buttonPanel.setBackground( Color.whit e);
buttonPanel.add(printButto n);
buttonPanel.add(exit);
content.add(buttonPanel, BorderLayout.SOUTH);
jtp = new JPrintableEditPane();
jtp.setEditorKit(new HTMLEditorKit());
try{
jtp.setPage("file:///c:\\I O.html");
jtp.setEditable(false);
JScrollPane editorScrollPane = new JScrollPane(jtp);
editorScrollPane.setVertic alScrollBa rPolicy(
JScrollPane.VERTICAL_SCROL LBAR_ALWAY S);
editorScrollPane.setPrefer redSize(ne w Dimension(250, 145));
content.add(editorScrollPa ne, BorderLayout.CENTER);
}
catch(IOException e){
System.out.println("Print Error : "+e);
}
setVisible(true);
}
public void actionPerformed(ActionEven t event) {
if (event.getSource()==printB utton)
{
DocFlavor flavor = null;//or the flavor you need
HashPrintRequestAttributeS et pras = new HashPrintRequestAttributeS et();
PrintService printServices[] =
PrintServiceLookup.lookupP rintServic es(flavor, null);
for (int i=0;i<4;i++)
System.out.println(printSe rvices[i]) ;
DocPrintJob job = printServices[1].createPri ntJob();
Doc doc = new SimpleDoc(jtp, flavor, new HashDocAttributeSet());
try {
job.print(doc, pras);
} catch (PrintException ex) {
ex.printStackTrace();
}
}
this.hide();
}
public static void main(String args[])
{
TestReport tr=new TestReport();
}
}
// END OF PROGRAM
/*************************
// TestReport.java
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.print.*;
import java.io.Serializable;
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.io.*;
import java.util.Iterator;
import javax.imageio.*;
import javax.imageio.stream.Image
import javax.print.*;
import javax.print.attribute.Hash
import javax.print.attribute.Hash
import java.awt.image.BufferedIma
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()
{
setSize(800,550);
calendar=Calendar.getInsta
day = calendar.get(Calendar.DATE
month= calendar.get(Calendar.MONT
year = calendar.get(Calendar.YEAR
String todate=year+"-"+month+"-"+
str="";
str=str+"<HTML><HEAD></HEA
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>";
str=str+"</body></html>";
try
{
FileWriter f1=new FileWriter("c:\\IO.html");
for (int i=0;i<str.length();++i)
f1.write(str.charAt(i)); f1.close();
}
catch(Exception e)
{System.out.println("Error
content = getContentPane();
printButton = new JButton("Print");
exit = new JButton ("Close");
printButton.addActionListe
exit.addActionListener(thi
JPanel buttonPanel = new JPanel();
buttonPanel.setBackground(
buttonPanel.add(printButto
buttonPanel.add(exit);
content.add(buttonPanel, BorderLayout.SOUTH);
jtp = new JPrintableEditPane();
jtp.setEditorKit(new HTMLEditorKit());
try{
jtp.setPage("file:///c:\\I
jtp.setEditable(false);
JScrollPane editorScrollPane = new JScrollPane(jtp);
editorScrollPane.setVertic
JScrollPane.VERTICAL_SCROL
editorScrollPane.setPrefer
content.add(editorScrollPa
}
catch(IOException e){
System.out.println("Print Error : "+e);
}
setVisible(true);
}
public void actionPerformed(ActionEven
if (event.getSource()==printB
{
DocFlavor flavor = null;//or the flavor you need
HashPrintRequestAttributeS
PrintService printServices[] =
PrintServiceLookup.lookupP
for (int i=0;i<4;i++)
System.out.println(printSe
DocPrintJob job = printServices[1].createPri
Doc doc = new SimpleDoc(jtp, flavor, new HashDocAttributeSet());
try {
job.print(doc, pras);
} catch (PrintException ex) {
ex.printStackTrace();
}
}
this.hide();
}
public static void main(String args[])
{
TestReport tr=new TestReport();
}
}
// END OF PROGRAM
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
sorry, please replace the code
DocPrintJob job = printServices[1].createPri ntJob();
with this code
DocPrintJob job = printServices[0].createPri ntJob();
-gnoon-
DocPrintJob job = printServices[1].createPri
with this code
DocPrintJob job = printServices[0].createPri
-gnoon-
ASKER
Rupa