I also cannot print an extremely simple Java application
after upgrading to Service Pack 2. Does anyone know
specifically about Java apps? Here is some example source
code that hangs with Service Pack 2 (when user is logged
in over the network).
import javax.swing.*;
import javax.print.DocFlavor;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.ServiceUI;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.HashPrintRequestAttributeSet;
import java.awt.*;
import java.awt.print.Printable;
import java.awt.print.PageFormat;
import java.awt.print.PrinterException;
public class PrintTest{
private JFrame fFrame;
private MyTextArea fTextArea;
public PrintTest(){
//bring up a 1.4 print dialog for a text area.
fFrame = new JFrame("Text Area");
fTextArea = new MyTextArea("foo bar test\n" +
"foo bar test\n" +
"test test test");
fFrame.getContentPane().add(fTextArea);
fFrame.setSize(200, 200);
fFrame.setVisible(true);
print();
}
public void print(){
DocFlavor flavor =
DocFlavor.SERVICE_FORMATTED.PRINTABLE;
PrintService defaultService =
PrintServiceLookup.lookupDefaultPrintService();
final PrintRequestAttributeSet pras = new
HashPrintRequestAttributeSet();
PrintService[] printService = // using pras above
causes problems-- no printers returned
PrintServiceLookup.lookupPrintServices
(flavor, new HashPrintRequestAttributeSet());
Point location = fFrame.getLocationOnScreen();
ServiceUI.printDialog(null, location.x + 100,
location.y + 100,
printService, defaultService, flavor,
pras);
}
public class MyTextArea extends JTextArea implements
Printable{
public MyTextArea(String text){
super(text);
}
public int print(Graphics g, PageFormat pf, int
pageIndex) throws PrinterException{
return -1;
}
}
public static void main(String[] args){
new PrintTest();
}
}