How to print a Panel in java

To print a panel in java there are some steps required to print panel and these easy to implement in your program that are given below you find that code and paste into your code and you will get print option in your system.







Step 1:

Make sure your panel is ready that means all components are arranged  correctly.

Step 2:

In that panel you also put a button with name 'print' and to addActionLestener(-) method to it.

Step3:

In actionPerformed (-) method you can write the below code.
public void actionPerformed(ActionEvent e) 
{
 PrinterJob printJob = PrinterJob.getPrinterJob();
 printJob.setPrintable(this);
 if (printJob.printDialog())
 {

  try
  {

   printJob.print();
  }
  catch(Exception ex)
  {

   throw new RuntimeException(ex);
  }
 }

 }




Step4: 

Then add the below code also in your program.
public int print(Graphics g, PageFormat pf, int index) throws PrinterException 
 {
 Graphics2D g2 = (Graphics2D)g;

   if (index >= 1)
   {
     return Printable.NO_SUCH_PAGE;
   }
   else 
   {
   this.printAll(g2);

     return Printable.PAGE_EXISTS;
 }

}