Solved

Automatic Selection of Printer not working!

Posted on 2003-11-02
3
220 Views
Last Modified: 2010-03-31
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.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></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>";
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.addActionListener(this);
   exit.addActionListener(this);
   JPanel 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.VERTICAL_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[i]);
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
0
Comment
Question by:rupasuraj
  • 2
3 Comments
 

Author Comment

by:rupasuraj
ID: 9668945
Any experts please go through my query and suggest a solution...

Rupa
0
 
LVL 16

Accepted Solution

by:
gnoon earned 50 total points
ID: 9677207
try this actionPerformed :)

public void actionPerformed(ActionEvent event) {
        if (event.getSource()==printButton)
        {
            DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE;
            PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
            pras.add(OrientationRequested.LANDSCAPE);
            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++)
                System.out.println(printServices[i].getName());
            DocPrintJob job = printServices[1].createPrintJob();
            Doc doc = new SimpleDoc(jtp, flavor,null);
            try {
                 job.print(doc, pras);
            } catch (PrintException ex) {
                    ex.printStackTrace();
            }
        }
        this.hide();
}
0
 
LVL 16

Expert Comment

by:gnoon
ID: 9677224
sorry, please replace the code

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

with this code

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

-gnoon-
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
groupSum6 challenge 6 77
Eclipse IDE - Cannot copy/paste from console output 8 127
eclipse argument 14 53
difference of if loops 23 41
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now