Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 13 of 13

Thread: Java Printer Job

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java Printer Job

    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 }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,979
    Thanks
    56
    Thanked 2,359 Times in 2,330 Posts

    Default Re: Java Printer Job

    Please edit the post, remove the line numbers and wrap the code in code tags.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Dec 2012
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Printer Job

    //Import packages
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.print.*;

    public class Program1{
    //Static swing components
    static JFrame frmMain;
    static Container pane;
    static JButton btnPrint;

    public static void main (String[] args){
    //Apply system look and feel
    try {UIManager.setLookAndFeel(UIManager.getSystemLookA ndFeelClassName());}
    catch (Exception e){}

    //Create and resize frame
    frmMain = new JFrame ("Sample printing application");
    frmMain.setSize(300, 200); //300x200 pixels
    pane = frmMain.getContentPane();
    pane.setLayout(null); //Use null layout

    //Create components
    btnPrint = new JButton ("Print"); //Create our button

    //Add components to pane
    pane.add(btnPrint);
    //Set components' bounds
    btnPrint.setBounds(5, 5, 100, 25); //Arguments: x, y, width, height

    //Make frame visible
    frmMain.setVisible(true);

    //Add the button's action
    btnPrint.addActionListener(new btnPrintAction());
    }

    public static class btnPrintAction implements ActionListener, Printable{
    public int print(Graphics gx, PageFormat pf, int page) throws PrinterException {
    if (page>0){return NO_SUCH_PAGE;} //Only one page
    Graphics2D g = (Graphics2D)gx; //Cast to Graphics2D object
    g.translate(pf.getImageableX(), pf.getImageableY()); //Match origins to imageable area
    g.drawString ("Hello world", 100, 100); //Print Hello World at offset (100, 100)
    return PAGE_EXISTS; //Page exists (offsets start at zero!)
    }
    public void actionPerformed(ActionEvent e) {
    PrinterJob job = PrinterJob.getPrinterJob(); //Get the printer's job list
    job.setPrintable(this); //We print with this class (btnPrintAction, which implements Printable)
    if (job.printDialog() == true) { //If we clicked OK in the print dialog
    try {job.print();} catch (PrinterException ex){
    //It did not work (PrinterException thrown), so add any error handling routines.
    }
    }
    }
    }
    }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,979
    Thanks
    56
    Thanked 2,359 Times in 2,330 Posts

    Default Re: Java Printer Job

    That's part of it, now Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.

    Make sure the code is properly formatted. The statements should NOT all start in the first column.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Dec 2012
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Printer Job

    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.print.PageFormat;
    import java.awt.print.Printable;
    import java.awt.print.PrinterException;
    import java.awt.print.PrinterJob;
     
    /**
     *
     * @author Amir
     */
    public class btnPrintAction implements ActionListener, Printable{
        /*to be able to get something out of your printer,you need a class
        *which implements the printable interface.
        */
        @Override
        public void actionPerformed(ActionEvent e) {
            /*Action happens when you click the button!
            *the actionPerformed method is called when you click the button.
            * when you click the button, a print dialog should pop
            */
            PrinterJob printerjob = PrinterJob.getPrinterJob();//get the printer job list
            printerjob.setPrintable(this);//we print with this class(btnPrintAction)
            if(printerjob.printDialog()==true){
                try{
                    System.out.println("ijwhdfkjbkfvn");
                    printerjob.print();
                }
                catch(PrinterException ex){
                    System.out.println("eshkal az injas");
                    //ex.getMessage();
                }
            }
        }
     
     
        /**
         *
         * @return
         */    
        @Override
        public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
            System.out.println("moshkel");
    	Graphics2D g = (Graphics2D)graphics; //Cast to Graphics2D object
    	g.translate(pageFormat.getImageableX(), pageFormat.getImageableY()); //Match origins to imageable area
    	g.drawString ("Hello world", 100, 100); //Print Hello World at offset (100, 100)
    	return PAGE_EXISTS; //Page exists (offsets start at zero!)
        }
    }

    import java.awt.Container;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    /**
     *
     * @author Amir
     */
    public class SaleServiceReciept {
     
        //Static swing components
        static JFrame frmMain;
        static Container pane;
        static JButton btnPrint;
     
        public static void main(String[] args) {
     
    //Create and resize frame
            frmMain = new JFrame("Sample printing application");
            frmMain.setSize(300, 200); //300x200 pixels
            pane = frmMain.getContentPane();
            pane.setLayout(null); //Use null layout
     
            //Create components
            btnPrint = new JButton("Print"); //Create our button
     
            //Add components to pane
            pane.add(btnPrint);
     
            //Set components' bounds
            btnPrint.setBounds(5, 5, 100, 25); //Arguments: x, y, width, height
     
            //Make frame visible
            frmMain.setVisible(true);
     
            //Add the button's action
            btnPrint.addActionListener(new btnPrintAction());
        }
    }

  6. #6
    Junior Member
    Join Date
    Dec 2012
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Printer Job

     
    public class SaleServiceReciept {
     
        //Static swing components
        static JFrame frmMain;
        static Container pane;
        static JButton btnPrint;
     
        public static void main(String[] args) {
     
    //Create and resize frame
            frmMain = new JFrame("Sample printing application");
            frmMain.setSize(300, 200); //300x200 pixels
            pane = frmMain.getContentPane();
            pane.setLayout(null); //Use null layout
     
            //Create components
            btnPrint = new JButton("Print"); //Create our button
     
            //Add components to pane
            pane.add(btnPrint);
     
            //Set components' bounds
            btnPrint.setBounds(5, 5, 100, 25); //Arguments: x, y, width, height
     
            //Make frame visible
            frmMain.setVisible(true);
     
            //Add the button's action
            btnPrint.addActionListener(new btnPrintAction());
        }
    }


    --- Update ---

     
    public class btnPrintAction implements ActionListener, Printable{
        /*to be able to get something out of your printer,you need a class
        *which implements the printable interface.
        */
        @Override
        public void actionPerformed(ActionEvent e) {
            /*Action happens when you click the button!
            *the actionPerformed method is called when you click the button.
            * when you click the button, a print dialog should pop
            */
            PrinterJob printerjob = PrinterJob.getPrinterJob();//get the printer job list
            printerjob.setPrintable(this);//we print with this class(btnPrintAction)
            if(printerjob.printDialog()==true){
                try{
                    System.out.println("ijwhdfkjbkfvn");
                    printerjob.print();
                }
                catch(PrinterException ex){
                    System.out.println("eshkal az injas");
                    //ex.getMessage();
                }
            }
        }
     
     
        /**
         *
         * @return
         */    
        @Override
        public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
            System.out.println("moshkel");
    	Graphics2D g = (Graphics2D)graphics; //Cast to Graphics2D object
    	g.translate(pageFormat.getImageableX(), pageFormat.getImageableY()); //Match origins to imageable area
    	g.drawString ("Hello world", 100, 100); //Print Hello World at offset (100, 100)
    	return PAGE_EXISTS; //Page exists (offsets start at zero!)
        }
    }

  7. #7
    Junior Member
    Join Date
    Dec 2012
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Printer Job

     
    public class btnPrintAction implements ActionListener, Printable{
        /*to be able to get something out of your printer,you need a class
        *which implements the printable interface.
        */
        @Override
        public void actionPerformed(ActionEvent e) {
            /*Action happens when you click the button!
            *the actionPerformed method is called when you click the button.
            * when you click the button, a print dialog should pop
            */
            PrinterJob printerjob = PrinterJob.getPrinterJob();//get the printer job list
            printerjob.setPrintable(this);//we print with this class(btnPrintAction)
            if(printerjob.printDialog()==true){
                try{
                    System.out.println("ijwhdfkjbkfvn");
                    printerjob.print();
                }
                catch(PrinterException ex){
                    System.out.println("eshkal az injas");
                    //ex.getMessage();
                }
            }
        }
     
     
     
        @Override
        public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
            System.out.println("moshkel");
    	Graphics2D g = (Graphics2D)graphics; //Cast to Graphics2D object
    	g.translate(pageFormat.getImageableX(), pageFormat.getImageableY()); //Match origins to imageable area
    	g.drawString ("Hello world", 100, 100); //Print Hello World at offset (100, 100)
    	return PAGE_EXISTS; //Page exists (offsets start at zero!)
        }
    }

  8. #8
    Junior Member
    Join Date
    Dec 2012
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Printer Job

     
    public class btnPrintAction implements ActionListener, Printable{
        /*to be able to get something out of your printer,you need a class
        *which implements the printable interface.
        */
        @Override
        public void actionPerformed(ActionEvent e) {
            /*Action happens when you click the button!
            *the actionPerformed method is called when you click the button.
            * when you click the button, a print dialog should pop
            */
            PrinterJob printerjob = PrinterJob.getPrinterJob();//get the printer job list
            printerjob.setPrintable(this);//we print with this class(btnPrintAction)
            if(printerjob.printDialog()==true){
                try{
                    System.out.println("ijwhdfkjbkfvn");
                    printerjob.print();
                }
                catch(PrinterException ex){
                    System.out.println("eshkal az injas");
                    //ex.getMessage();
                }
            }
        }
     
     
        /**
         *
         * @return
         */    
        @Override
        public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
            System.out.println("moshkel");
    	Graphics2D g = (Graphics2D)graphics; //Cast to Graphics2D object
    	g.translate(pageFormat.getImageableX(), pageFormat.getImageableY()); //Match origins to imageable area
    	g.drawString ("Hello world", 100, 100); //Print Hello World at offset (100, 100)
    	return PAGE_EXISTS; //Page exists (offsets start at zero!)
        }
    }

  9. #9
    Junior Member
    Join Date
    Dec 2012
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Printer Job

    public class btnPrintAction implements ActionListener, Printable{
        /*to be able to get something out of your printer,you need a class
        *which implements the printable interface.
        */
        @Override
        public void actionPerformed(ActionEvent e) {
            /*Action happens when you click the button!
            *the actionPerformed method is called when you click the button.
            * when you click the button, a print dialog should pop
            */
            PrinterJob printerjob = PrinterJob.getPrinterJob();//get the printer job list
            printerjob.setPrintable(this);//we print with this class(btnPrintAction)
            if(printerjob.printDialog()==true){
                try{
                    System.out.println("ijwhdfkjbkfvn");
                    printerjob.print();
                }
                catch(PrinterException ex){
                    System.out.println("eshkal az injas");
                    //ex.getMessage();
                }
            }
        }
     
     
     
        @Override
        public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
            System.out.println("moshkel");
    	Graphics2D g = (Graphics2D)graphics; //Cast to Graphics2D object
    	g.translate(pageFormat.getImageableX(), pageFormat.getImageableY()); //Match origins to imageable area
    	g.drawString ("Hello world", 100, 100); //Print Hello World at offset (100, 100)
    	return PAGE_EXISTS; //Page exists (offsets start at zero!)
        }
    }

  10. #10
    Junior Member
    Join Date
    Dec 2012
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Printer Job

     
    public class SaleServiceReciept {
     
        //Static swing components
        static JFrame frmMain;
        static Container pane;
        static JButton btnPrint;
     
        public static void main(String[] args) {
     
            //Create and resize frame
            frmMain = new JFrame("Sample printing application");
            frmMain.setSize(300, 200); //300x200 pixels
            pane = frmMain.getContentPane();
            pane.setLayout(null); //Use null layout
     
            //Create components
            btnPrint = new JButton("Print"); //Create our button
     
            //Add components to pane
            pane.add(btnPrint);
     
            //Set components' bounds
            btnPrint.setBounds(5, 5, 100, 25); //Arguments: x, y, width, height
     
            //Make frame visible
            frmMain.setVisible(true);
     
            //Add the button's action
            btnPrint.addActionListener(new btnPrintAction());
        }
    }
     
    public class btnPrintAction implements ActionListener, Printable{
        /*to be able to get something out of your printer,you need a class
        *which implements the printable interface.
        */
        @Override
        public void actionPerformed(ActionEvent e) {
            /*Action happens when you click the button!
            *the actionPerformed method is called when you click the button.
            * when you click the button, a print dialog should pop
            */
            PrinterJob printerjob = PrinterJob.getPrinterJob();//get the printer job list
            printerjob.setPrintable(this);//we print with this class(btnPrintAction)
            if(printerjob.printDialog()==true){
                try{
                    System.out.println("ijwhdfkjbkfvn");
                    printerjob.print();
                }
                catch(PrinterException ex){
                    System.out.println("eshkal az injas");
                    //ex.getMessage();
                }
            }
        }
     
        @Override
        public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
            System.out.println("moshkel");
    	Graphics2D g = (Graphics2D)graphics; //Cast to Graphics2D object
    	g.translate(pageFormat.getImageableX(), pageFormat.getImageableY()); //Match origins to imageable area
    	g.drawString ("Hello world", 100, 100); //Print Hello World at offset (100, 100)
    	return PAGE_EXISTS; //Page exists (offsets start at zero!)
        }
    }
    Last edited by amirham; December 8th, 2012 at 01:14 AM. Reason: add another class

  11. #11
    Junior Member
    Join Date
    Dec 2012
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Printer Job

     
    public class SaleServiceReciept {
     
        //Static swing components
        static JFrame frmMain;
        static Container pane;
        static JButton btnPrint;
     
        public static void main(String[] args) {
     
            //Create and resize frame
            frmMain = new JFrame("Sample printing application");
            frmMain.setSize(300, 200); //300x200 pixels
            pane = frmMain.getContentPane();
            pane.setLayout(null); //Use null layout
     
            //Create components
            btnPrint = new JButton("Print"); //Create our button
     
            //Add components to pane
            pane.add(btnPrint);
     
            //Set components' bounds
            btnPrint.setBounds(5, 5, 100, 25); //Arguments: x, y, width, height
     
            //Make frame visible
            frmMain.setVisible(true);
     
            //Add the button's action
            btnPrint.addActionListener(new btnPrintAction());
        }
    }
     
     
    public class btnPrintAction implements ActionListener, Printable{
        /*to be able to get something out of your printer,you need a class
        *which implements the printable interface.
        */
        @Override
        public void actionPerformed(ActionEvent e) {
            /*Action happens when you click the button!
            *the actionPerformed method is called when you click the button.
            * when you click the button, a print dialog should pop
            */
            PrinterJob printerjob = PrinterJob.getPrinterJob();//get the printer job list
            printerjob.setPrintable(this);//we print with this class(btnPrintAction)
            if(printerjob.printDialog()==true){
                try{
                    System.out.println("ijwhdfkjbkfvn");
                    printerjob.print();
                }
                catch(PrinterException ex){
                    System.out.println("eshkal az injas");
                    //ex.getMessage();
                }
            }
        }
     
        @Override
        public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
            System.out.println("moshkel");
    	Graphics2D g = (Graphics2D)graphics; //Cast to Graphics2D object
    	g.translate(pageFormat.getImageableX(), pageFormat.getImageableY()); //Match origins to imageable area
    	g.drawString ("Hello world", 100, 100); //Print Hello World at offset (100, 100)
    	return PAGE_EXISTS; //Page exists (offsets start at zero!)
        }
    }

  12. #12
    Junior Member
    Join Date
    Dec 2012
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Printer Job

    the moderator doesnt accept the code...i dunno why!?

    --- Update ---

     
    public class SaleServiceReciept {
     
        //Static swing components
        static JFrame frmMain;
        static Container pane;
        static JButton btnPrint;
     
        public static void main(String[] args) {
     
            //Create and resize frame
            frmMain = new JFrame("Sample printing application");
            frmMain.setSize(300, 200); //300x200 pixels
            pane = frmMain.getContentPane();
            pane.setLayout(null); //Use null layout
     
            //Create components
            btnPrint = new JButton("Print"); //Create our button
     
            //Add components to pane
            pane.add(btnPrint);
     
            //Set components' bounds
            btnPrint.setBounds(5, 5, 100, 25); //Arguments: x, y, width, height
     
            //Make frame visible
            frmMain.setVisible(true);
     
            //Add the button's action
            btnPrint.addActionListener(new btnPrintAction());
        }
    }


    --- Update ---

     
    public class btnPrintAction implements ActionListener, Printable{
        /*to be able to get something out of your printer,you need a class
        *which implements the printable interface.
        */
        @Override
        public void actionPerformed(ActionEvent e) {
            /*Action happens when you click the button!
            *the actionPerformed method is called when you click the button.
            * when you click the button, a print dialog should pop
            */
            PrinterJob printerjob = PrinterJob.getPrinterJob();//get the printer job list
            printerjob.setPrintable(this);//we print with this class(btnPrintAction)
            if(printerjob.printDialog()==true){
                try{
                    System.out.println("ijwhdfkjbkfvn");
                    printerjob.print();
                }
                catch(PrinterException ex){
                    System.out.println("eshkal az injas");
                    //ex.getMessage();
                }
            }
        }
     
        @Override
        public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
            System.out.println("moshkel");
    	Graphics2D g = (Graphics2D)graphics; //Cast to Graphics2D object
    	g.translate(pageFormat.getImageableX(), pageFormat.getImageableY()); //Match origins to imageable area
    	g.drawString ("Hello world", 100, 100); //Print Hello World at offset (100, 100)
    	return PAGE_EXISTS; //Page exists (offsets start at zero!)
        }
    }


    --- Update ---

    here is the code ...can you help me on this!!!???

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,979
    Thanks
    56
    Thanked 2,359 Times in 2,330 Posts

    Default Re: Java Printer Job

    The code executes without ending when I try it? It prints:

    ijwhdfkjbkfvn
    moshkel
    moshkel
    ..... and many more
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Java to printer ?
    By KZaki in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: November 26th, 2012, 01:48 PM
  2. how get good java job
    By gokul in forum Member Introductions
    Replies: 1
    Last Post: April 30th, 2012, 06:56 AM
  3. Java Vierual Printer
    By abu_alfouz in forum Member Introductions
    Replies: 0
    Last Post: July 25th, 2011, 09:16 AM
  4. Java printer selection
    By pnvsgupta in forum Java SE APIs
    Replies: 4
    Last Post: July 23rd, 2011, 12:31 PM
  5. java job search
    By medasi in forum Java Theory & Questions
    Replies: 0
    Last Post: January 18th, 2011, 03:09 AM