The Artima Developer Community
Sponsored Link

Java Answers Forum
Automatic Selection of Printer not working

1 reply on 1 page. Most recent reply: Nov 3, 2003 2:10 PM by David

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Suresh Babu

Posts: 2
Nickname: sureshbvs
Registered: Nov, 2003

Automatic Selection of Printer not working Posted: Nov 3, 2003 1:48 AM
Reply to this message Reply
Advertisement
According to the instructions that I got from some experts 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.ImageInputStream;
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()
{
setSize(800,550);
calendar=Calendar.getInstance();
day = calendar.get(Calendar.DATE) ;
month= calendar.get(Calendar.MONTH)+1 ;
year = calendar.get(Calendar.YEAR);
String todate=year+"-"+month+"-"+day;
str="";
str=str+"<HTML><HEAD></HE AD><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
{
File Writer 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.addActionListener(this);
exit.addActionListener(this);
J Panel buttonPanel = new JPanel();
buttonPanel.setBackground(Color.white);
buttonPanel.add(printButton);
buttonPanel.add(exit);
content.add(buttonPanel, BorderLayout.SOUTH);
jtp = new JPrintableEditPane();
jtp.setEditorKit(new HTMLEditorKit());

try{
jtp.setPage("file:///c:\\IO.html");
jtp.setEditable(false);
JScrollPane editorScrollPane = new JScrollPane(jtp);
editorScrollPane.setVerticalScrollBarPolicy(
JScrollPane.VERT ICAL_SCROLLBAR_ALWAYS);
editorScrollPane.setPreferredSize(new Dimension(250, 145));
content.add(editorScrollPane, BorderLayout.CENTER);
}
catch(IOException e){
System.out.println("Print Error : "+e);
}
setVisible(true);
}

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++)
System.out.println(printServices);
DocPrintJob job = printServices[1].createPrintJob();
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


David

Posts: 150
Nickname: archangel
Registered: Jul, 2003

Re: Automatic Selection of Printer not working Posted: Nov 3, 2003 2:10 PM
Reply to this message Reply
It helps if you post the complete stack trace of the Exception you received.

Flat View: This topic has 1 reply on 1 page
Topic: a menthod to print a token by token each time you call Previous Topic   Next Topic Topic: Dividing 2 double values

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use