Try to print a "hello world"
but an exception occured as follow:
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 7
at sun.print.Win32PrintService.getMediaTrays(Win32Pri ntService.java:376)
at sun.print.Win32PrintService.getSupportedAttributeV alues(Win32PrintService.java:1152)
at sun.print.RasterPrinterJob.updatePageAttributes(Ra sterPrinterJob.java:519)
at sun.print.RasterPrinterJob.setPrintable(RasterPrin terJob.java:966)
at sun.print.RasterPrinterJob.setAttributes(RasterPri nterJob.java:1203)
at sun.awt.windows.WPrinterJob.setAttributes(WPrinter Job.java:610)
at sun.print.RasterPrinterJob.print(RasterPrinterJob. java:1301)
at sun.print.RasterPrinterJob.print(RasterPrinterJob. java:1247)
at infotech.btnPrintAction.actionPerformed(btnPrintAc tion.java:31)
at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.jav a:6288)
at javax.swing.JComponent.processMouseEvent(JComponen t.java:3267)
at java.awt.Component.processEvent(Component.java:605 3)
at java.awt.Container.processEvent(Container.java:204 1)
at java.awt.Component.dispatchEventImpl(Component.jav a:4651)
at java.awt.Container.dispatchEventImpl(Container.jav a:2099)
at java.awt.Component.dispatchEvent(Component.java:44 81)
at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4577)
at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:4238)
at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:4168)
at java.awt.Container.dispatchEventImpl(Container.jav a:2085)
at java.awt.Window.dispatchEventImpl(Window.java:2478 )
at java.awt.Component.dispatchEvent(Component.java:44 81)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.j ava:643)
at java.awt.EventQueue.access$000(EventQueue.java:84)
at java.awt.EventQueue$1.run(EventQueue.java:602)
at java.awt.EventQueue$1.run(EventQueue.java:600)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:87)
at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:98)
at java.awt.EventQueue$2.run(EventQueue.java:616)
at java.awt.EventQueue$2.run(EventQueue.java:614)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:87)
at java.awt.EventQueue.dispatchEvent(EventQueue.java: 613)
at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:122)
The code is as follows:
01 //Import packages
02 import javax.swing.*;
03 import java.awt.*;
04 import java.awt.event.*;
05 import java.awt.print.*;
06
07 public class Program1{
08 //Static swing components
09 static JFrame frmMain;
10 static Container pane;
11 static JButton btnPrint;
12
13 public static void main (String[] args){
14 //Apply system look and feel
15 try {UIManager.setLookAndFeel(UIManager.getSystemLookA ndFeelClassName());}
16 catch (Exception e){}
17
18 //Create and resize frame
19 frmMain = new JFrame ("Sample printing application");
20 frmMain.setSize(300, 200); //300x200 pixels
21 pane = frmMain.getContentPane();
22 pane.setLayout(null); //Use null layout
23
24 //Create components
25 btnPrint = new JButton ("Print"); //Create our button
26
27 //Add components to pane
28 pane.add(btnPrint);
29
30 //Set components' bounds
31 btnPrint.setBounds(5, 5, 100, 25); //Arguments: x, y, width, height
32
33 //Make frame visible
34 frmMain.setVisible(true);
35
36 //Add the button's action
37 btnPrint.addActionListener(new btnPrintAction());
38 }
39
40 public static class btnPrintAction implements ActionListener, Printable{
41 public int print(Graphics gx, PageFormat pf, int page) throws PrinterException {
42 if (page>0){return NO_SUCH_PAGE;} //Only one page
43 Graphics2D g = (Graphics2D)gx; //Cast to Graphics2D object
44 g.translate(pf.getImageableX(), pf.getImageableY()); //Match origins to imageable area
45 g.drawString ("Hello world", 100, 100); //Print Hello World at offset (100, 100)
46 return PAGE_EXISTS; //Page exists (offsets start at zero!)
47 }
48 public void actionPerformed(ActionEvent e) {
49 PrinterJob job = PrinterJob.getPrinterJob(); //Get the printer's job list
50 job.setPrintable(this); //We print with this class (btnPrintAction, which implements Printable)
51 if (job.printDialog() == true) { //If we clicked OK in the print dialog
52 try {job.print();} catch (PrinterException ex){
53 //It did not work (PrinterException thrown), so add any error handling routines.
54 }
55 }
56 }
57 }
58 }