I'm not able to get the printout.

here is the code

public class Billing extends Frame implements ActionListener,Printable
{
JButton printbtn = new JButton("Print");


}
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex)
            throws PrinterException
            {
        if (pageIndex > 0)
        {
             return NO_SUCH_PAGE;
        }

        // User (0,0) is typically outside the
        // imageable area, so we must translate
        // by the X and Y values in the PageFormat
        // to avoid clipping.
        Graphics2D g2d = (Graphics2D)graphics;

        g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());

        // Now we perform our rendering
        graphics.drawString("Hello world!", 100, 100);

        // tell the caller that this page is part
        // of the printed document
        return PAGE_EXISTS;
        // TODO Auto-generated method stub

    }

    public Billing
    (
    printbtn.addActionListener(this);
    JPanel panel = new JPanel();
    panel.setLayout(new MigLayout("debug,fillx,filly", "[]rel:push[]", "[]1[]"));
    panel.add(printbtn);
    frame.add(panel);   
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.pack();        
        frame.setVisible(true);

    )
    @Override
    public void actionPerformed(ActionEvent e) 
    {
    else if (e.getSource().equals(printbtn))
        {
            System.out.println("You have clicked on Print Button!");            
            PrinterJob pj = PrinterJob.getPrinterJob();
            job.setPrintable(this);
            if (pj.printDialog() == true)
             {
                    try 
                    {
                        pj.print();

                        }
                    catch (PrinterException exc)
                    {
                        System.out.println(exc);
                     }
                 }   

            System.out.println("Print Job Ends Here!!!!XXXXX!!!!");

        }


    }

It doesn't print because it doesn't run because it doesn't compile!

Read all the compiler error messages, fix all the compile errors, and maybe you will get a bit further.

There are no compiler messages.

sorry I forgot to post the main method

public static void main(String[] args)
    {       

         try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } 
         catch (Exception e)
         {
            }   
        //new TabColors().display();
                new Billing();

    }

There are no compiler messages.

Look again...
Your class definition ends on line 6 - how did you compile what follows?
Line 33 is invalid syntax - how did you compile that?
(etc)

And your main method makes things a LOT worse...
How many times have we said this in the Java forum...
Never NEVER NEVER do this when writing new code:

} catch (Exception e) {
}

If/when there is an error you just told Java that you didn't want to know anything about it, and please discard the detailed error message that Java just created for you.
ALWAYS put an e.printStackTrace(); in your catch blocks until/unless you have a good reason to do something else.

I'm sorry once again for the bad Copy/Paste.
I hope this time it's right.

public class Billing extends Frame implements ActionListener,Printable
    {
JButton printbtn = new JButton("Print");
    JFrame frame = new JFrame();

    PrinterJob job = PrinterJob.getPrinterJob();


    public int print(Graphics graphics, PageFormat pageFormat, int pageIndex)
            throws PrinterException
            {
        if (pageIndex > 0)
        {
             return NO_SUCH_PAGE;
        }

        // User (0,0) is typically outside the
        // imageable area, so we must translate
        // by the X and Y values in the PageFormat
        // to avoid clipping.
        Graphics2D g2d = (Graphics2D)graphics;
        /*Rectangle2D.Double rectangle = new Rectangle2D.Double ();
        rectangle.setRect (pageFormat.getImageableX () + 72,
                pageFormat.getImageableY () + 72,
                72,
                72);
g2d.draw (rectangle);*/
        g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());

        // Now we perform our rendering
        graphics.drawString("Hello world!", 100, 100);

        // tell the caller that this page is part
        // of the printed document
        return PAGE_EXISTS;
        // TODO Auto-generated method stub

    }

    public Billing() 
    {
        JPanel panel = new JPanel();            
        panel.setLayout(new MigLayout("debug,fillx,filly", "[]rel:push[]", "[]1[]")); 

        panel.add(printbtn);

       frame.add(panel);        
        printbtn.addActionListener(this);       
        Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize(); 
        int x=(int)((dimension.getWidth() - 760)/2);
        int y=(int)((dimension.getHeight() - 550)/2);
        frame.setLocation(x, y);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.pack();

        frame.setVisible(true);

    public static void main(String[] args)
    {       

         try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } 
         catch (Exception e)
         {
             e.printStackTrace();
            }   

                new Billing();

    }


    @Override
    public void actionPerformed(ActionEvent e) 
    {

         if (e.getSource().equals(printbtn))
        {
            System.out.println("You have clicked on Print Button!");            
            PrinterJob pj = PrinterJob.getPrinterJob();
            job.setPrintable(this);
            if (pj.printDialog() == true)
             {
                    try 
                    {
                        pj.print();

                        }
                    catch (PrinterException exc)
                    {
                        exc.printStackTrace();
                     }
                 }   

            System.out.println("Print Job Ends Here!!!!XXXXX!!!!");

        }   

    }

}   

}

I would suggest adding some additional println statements to the print method. Is it being reached? If it is, what is the value of pageindex when print gets called?

why use
if (pj.printDialog() == true)
instead of a simple
if (pj.printDialog())
have you checked whether your code goes into that if block? did you debug your code?

Yes my code is printing those two lines in the Console,so it must be going on that block.
1.You have clicked on Print Button!
2.Print Job Ends Here!!!!XXXXX!!!!

chdboy .... re-read your code. that is not the if-block I was talking about. I was talking about the one INSIDE that if-block.

The second if(if it's working will show me the printDialog box)is working besause it's showing me the printDialog.

so ... it works, then what is the problem?

The problem is that I wanted to print Hello World from this code

graphics.drawString("Hello world!", 100, 100);

And that is not happening.

I don't see you calling that method, that might explain a lot.

OK, here's what you did wrong...

In your class you created a printer job...

PrinterJob job = PrinterJob.getPrinterJob();

Then in the actionPerformed you created another one ...

PrinterJob pj = PrinterJob.getPrinterJob();

Then you proceed to mix the two up ...

job.setPrintable(this);
if (pj.printDialog() == true)
...  pj.print();

That's why your print doesn't work. I'm sure you can see how trivial the fix is, knowing that.

How to do that?and where?

I don't see you calling that method, that might explain a lot.

No, the code is correct (except for the error identified in my previous post). You call PrinterJob's setPrintable passing an instance of a Printable. The Printable interface specifies the print(etc) method, and so it's the PrinterJobb that calls his print method. An @Override annotation on his print method would have made this clearer...

Yes JamesCherrill you are right there were two jobs created and I was calling the wrong pj I should have called the right job.
This should be the code

job.setPrintable(this);
            if (job.printDialog())
             {
                    try 
                    {
                        job.print();

                        }
                    catch (PrinterException exc)
                    {
                        exc.printStackTrace();
                     }
                 }   

If I'm calling the

job.setPrintable(this);

then it should be job that I have to use not pj

thanks a lot :) .

Now I have to print a reciet using database values :)

Personally, the thing that surprised me here was that job.print(); doesn't throw a PrinterException if there is no Printable specified. That would have made it immediately obvious where to look. Oh well, maybe for Java 9?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.