When I press the print Button, all it prints is a blank page.
This is my code:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;

public class OptionWindow extends JFrame implements ActionListener
{



	private JLabel factoryprofitL,grossfactL,salesL,returninL,srtiL, openingstockL,purchL, retunoutL, carriageinL, closingstockL,costofgoodssoldL,grossprofitL,elecL,telephoneL,carriageoutwardL,waterL,expensetotalL,netproflossL;
	private JLabel headingL,expensesheadingL;
	private JTextField grossfactTF,factoryprofitTF,headingTF,salesTF,returninTF,srtiTF, openingstockTF,purchTF, retunoutTF, carriageinTF, closingstockTF,costofgoodssoldTF,grossprofitTF,elecTF,telephoneTF,carriageoutwardTF,waterTF,expensetotalTF,netproflossTF;

	private CalculateButtonHandlerTpl cbtHandler;
	private ExitButtonHandlerTpl ebtHandler;
	private JButton exitTB, calculateTB;

	private JButton printB;
private PrintButtonHandler pbHandler;
    public OptionWindow()
    {
		    setTitle("Trading Profit and Loss Account");
			salesL=new JLabel("Sales: \n",SwingConstants.RIGHT);
			returninL=new JLabel("Return Inwards: \n",SwingConstants.RIGHT);
			headingL=new JLabel("Cost of Goods Sold\n\n",SwingConstants.RIGHT);
			openingstockL=new JLabel("Opening Stock: \n",SwingConstants.RIGHT);
			factoryprofitL=new JLabel("Factory Profit/Loss: \n",SwingConstants.RIGHT);
			retunoutL=new JLabel("Return Outwards: \n",SwingConstants.RIGHT);
			carriageinL=new JLabel("Carriage Inwards: \n",SwingConstants.RIGHT);
			closingstockL=new JLabel("Closing Stock: \n",SwingConstants.RIGHT);

			grossprofitL=new JLabel("Gross Profit/Loss: \n",SwingConstants.RIGHT);
			expensesheadingL=new JLabel("Expenses\n",SwingConstants.RIGHT);
			elecL=new JLabel("Electricity: \n",SwingConstants.RIGHT);
			telephoneL=new JLabel("Telephone: \n",SwingConstants.RIGHT);
			carriageoutwardL=new JLabel("Carriage outwards: ",SwingConstants.RIGHT);
			waterL=new JLabel("Water: \n",SwingConstants.RIGHT);
			expensetotalL=new JLabel("Total expenses:\n",SwingConstants.RIGHT);
			netproflossL=new JLabel("Net Profit/Loss: \n",SwingConstants.RIGHT);

salesTF=new JTextField(10);
returninTF=new JTextField(10);
srtiTF=new JTextField(10);
//headingTF=new JTextField(10);
openingstockTF=new JTextField(10);
factoryprofitTF=new JTextField(10);
retunoutTF=new JTextField(10);
carriageinTF=new JTextField(10);
closingstockTF=new JTextField(10);
costofgoodssoldTF=new JTextField(10);
grossprofitTF=new JTextField(10);

elecTF=new JTextField(10);
telephoneTF=new JTextField(10);
carriageoutwardTF=new JTextField(10);
waterTF=new JTextField(10);
expensetotalTF=new JTextField(10);
netproflossTF=new JTextField(10);



		calculateTB=new JButton("Calculate");
		cbtHandler=new CalculateButtonHandlerTpl();
		calculateTB.addActionListener(cbtHandler);


		exitTB=new JButton("Exit");
		ebtHandler=new ExitButtonHandlerTpl();
		exitTB.addActionListener(ebtHandler);

printB=new JButton("Print");
pbHandler=new PrintButtonHandler();
printB.addActionListener(pbHandler);

		setTitle("Trading profit and loss account");

		Container pane=getContentPane();
		Container con=this.getContentPane();
		con.setBackground(Color.white);

		//pane.setLayout(new GridLayout(28,1));
con.setLayout(null);
con.setLayout(null );

		pane.add(salesL);
		pane.add(salesTF);
		salesL.setLocation(1,2);
    	salesL.setSize(190,25);
    	add(salesL);
        salesTF.setLocation(420,2);
    	salesTF.setSize(150,25);
    	add(salesTF);
		pane.add(returninL);
		pane.add(returninTF);
		returninL.setLocation(1,30);
		returninL.setSize(190,30);
		add(returninL);
		returninTF.setLocation(420,30);
		returninTF.setSize(150,25);
    	add(returninTF);


		pane.add(srtiTF);
		srtiTF.setLocation(420,58);
		srtiTF.setSize(150,25);
    	add(srtiTF);
    	srtiTF.setBorder(BorderFactory.createLoweredBevelBorder());
    srtiTF.setEditable(false);



		//pane.add(headingTF);
		pane.add(headingL);
		headingL.setLocation(1,86);
		headingL.setSize(150,30);
		add(headingL);

		pane.add(openingstockL);
		pane.add(openingstockTF);
		openingstockL.setLocation(1,114);
		openingstockL.setSize(190,30);
		add(openingstockL);
		openingstockTF.setLocation(250,114);
		openingstockTF.setSize(150,25);
    	add(openingstockTF);

		pane.add(factoryprofitL);
		pane.add(factoryprofitTF);
		factoryprofitL.setLocation(1,142);
		factoryprofitL.setSize(190,30);
		add(factoryprofitL);
		factoryprofitTF.setLocation(250,142);
		factoryprofitTF.setSize(150,25);
    	add(factoryprofitTF);

		pane.add(retunoutL);
		pane.add(retunoutTF);
		retunoutL.setLocation(1,170);
		retunoutL.setSize(190,30);
		add(retunoutL);
		retunoutTF.setLocation(250,170);
		retunoutTF.setSize(150,25);
    	add(retunoutTF);

		pane.add(carriageinL);
		pane.add(carriageinTF);
		carriageinL.setLocation(1,198);
		carriageinL.setSize(190,30);
		add(carriageinL);
		carriageinTF.setLocation(250,198);
		carriageinTF.setSize(150,25);
    	add(carriageinTF);

		pane.add(closingstockL);
		pane.add(closingstockTF);
		closingstockL.setLocation(1,226);
				closingstockL.setSize(190,30);
				add(closingstockL);
				closingstockTF.setLocation(250,226);
				closingstockTF.setSize(150,25);
    	add(closingstockTF);


		pane.add(costofgoodssoldTF);
		costofgoodssoldTF.setLocation(420,254);
		costofgoodssoldTF.setSize(150,25);
    	add(costofgoodssoldTF);
    	costofgoodssoldTF.setBorder(BorderFactory.createLoweredBevelBorder());
		    costofgoodssoldTF.setEditable(false);



		pane.add(grossprofitL);
		pane.add(grossprofitTF);
		grossprofitL.setLocation(1,282);
		grossprofitL.setSize(190,30);
		add(grossprofitL);
		grossprofitTF.setLocation(420,282);
		grossprofitTF.setSize(150,25);
    	add(grossprofitTF);
    	grossprofitTF.setBorder(BorderFactory.createLoweredBevelBorder());
		    grossprofitTF.setEditable(false);


		pane.add(expensesheadingL);
		expensesheadingL.setLocation(1,310);
		expensesheadingL.setSize(150,30);
		add(expensesheadingL);

		pane.add(elecL);
		pane.add(elecTF);
		elecL.setLocation(1,338);
		elecL.setSize(190,30);
		add(elecL);
		elecTF.setLocation(250,338);
		elecTF.setSize(150,25);
    	add(elecTF);

		pane.add(telephoneL);
		pane.add(telephoneTF);
		telephoneL.setLocation(1,366);
		telephoneL.setSize(190,30);
		add(telephoneL);
		telephoneTF.setLocation(250,366);
		telephoneTF.setSize(150,25);
    	add(telephoneTF);

		pane.add(carriageoutwardL);
		pane.add(carriageoutwardTF);
		carriageoutwardL.setLocation(1,394);
		carriageoutwardL.setSize(190,30);
		add(carriageoutwardL);
		carriageoutwardTF.setLocation(250,394);
		carriageoutwardTF.setSize(150,25);
    	add(carriageoutwardTF);

		pane.add(waterL);
		pane.add(waterTF);
		waterL.setLocation(1,422);
		waterL.setSize(190,30);
		add(waterL);
		waterTF.setLocation(250,422);
		waterTF.setSize(150,25);
    	add(waterTF);

		pane.add(expensetotalL);
		pane.add(expensetotalTF);
		expensetotalL.setLocation(1,460);
		expensetotalL.setSize(190,30);
		add(expensetotalL);
		expensetotalTF.setLocation(420,460);
		expensetotalTF.setSize(150,25);
		add(expensetotalTF);
		expensetotalTF.setBorder(BorderFactory.createLoweredBevelBorder());
		    expensetotalTF.setEditable(false);


		pane.add(netproflossL);
		pane.add(netproflossTF);
		netproflossL.setLocation(1,498);
		netproflossL.setSize(190,30);
		add(netproflossL);
		netproflossTF.setLocation(420,498);
		netproflossTF.setSize(150,25);
		add(netproflossTF);
		netproflossTF.setBorder(BorderFactory.createLoweredBevelBorder());
		netproflossTF.setEditable(false);

		pane.add(calculateTB);
		calculateTB.setLocation(420,533);
		calculateTB.setSize(150,30);
		add(calculateTB);


		pane.add(exitTB);
		exitTB.setLocation(420,570);
		exitTB.setSize(150,30);
		add(exitTB);

		pane.add(printB);
		printB.setLocation(120,570);
		printB.setSize(150,30);
		add(printB);

setSize(600,700);
		setVisible(true);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		}

		private class CalculateButtonHandlerTpl implements ActionListener
		{
			public void actionPerformed(ActionEvent e)
		{
			double sales=0.0;
			double o_stock=0.0;
			double p_s=0.0;
			double c_i=0.0;
			double c_stock=0.0;
			double r_o=0.0;
			double r_i=0.0;
			double cgs=0.0;
			double srti=0.0;
			double g_p=0.0;
			double factprof=0.0;



			double c_o=0.0;
			double n_p=0.0;
			double exp=0.0;
			double electricity=0.0;
			double telephone=0.0;
			double carriageout=0.0;
			double water=0.0;

		sales=Double.parseDouble(salesTF.getText());
		r_i=Double.parseDouble(returninTF.getText());
		o_stock=Double.parseDouble(openingstockTF.getText());
		r_o=Double.parseDouble(retunoutTF.getText());
		c_i=Double.parseDouble(carriageinTF.getText());
		c_stock=Double.parseDouble(closingstockTF.getText());
		factprof=Double.parseDouble(factoryprofitTF.getText());
		electricity=Double.parseDouble(elecTF.getText());
		telephone=Double.parseDouble(telephoneTF.getText());
		c_o=Double.parseDouble(carriageoutwardTF.getText());
		water=Double.parseDouble(waterTF.getText());


		srti=sales-r_i;
		cgs=o_stock+factprof-r_o+c_i-c_stock;
		g_p=srti-cgs;

		exp=telephone+electricity+c_o+water;
		n_p=g_p-exp;

		srtiTF.setText(" "+srti);
		costofgoodssoldTF.setText(" "+cgs);
		grossprofitTF.setText(" "+g_p);

		expensetotalTF.setText(" "+exp);
		netproflossTF.setText(" "+n_p);
			}
		}

private class PrintButtonHandler implements ActionListener, Printable
{//O

  public int print(Graphics g, PageFormat pf, int pageIndex) {
     if (pageIndex > 0) {
       return Printable.NO_SUCH_PAGE;
     }
     Graphics2D g2d = (Graphics2D) g;
     g2d.translate(pf.getImageableX(), pf.getImageableY());
     drawGraphics(g2d, pf);

     return Printable.PAGE_EXISTS;
   }
   public void drawGraphics(Graphics2D g2d,PageFormat pf){
   }
        public void actionPerformed(ActionEvent e)
        {

            PrinterJob job = PrinterJob.getPrinterJob();

            job.setPrintable(this);

            if (job.printDialog() == true)
            {
                try {job.print();} catch (PrinterException ex)
            {
       }
	}
}
}

		private class ExitButtonHandlerTpl implements ActionListener
		{
		public void actionPerformed(ActionEvent e)
		{
		System.exit(0);
		}
}
 public void actionPerformed(java.awt.event.ActionEvent e)
 {
 }
   public static void main(String[] args)
     {
         new OptionWindow();
 	}// TODO overwrite start(), stop() and destroy() methods
}

Its between line 338 and 366..
I feel like there has to be another statement to print all the visible..

oh i see it nvm... but what do i enter into that method to get it to print the GUI?

Still gives an error... cannot find variable frame...

Of course it can't find 'frame', unless you just happened to have a reference to your frame named 'frame' in your program. You can't simply paste their code into your program and expect it to work without bothering to understand what it's doing.

You need to call that method on a reference to the window you are wanting to print.

at line 28 i added

private JFrame frameToPrint;

and at line 346 i added

frameToPrint.printAll(g);

But when i press the print button now it gives an exception in thread

Probably because you have not actually set "frameToPrint" to your frame. You could create a method to do that or you could pass a "this" reference to your print button handler constructor to set it.

and how do i use the "this" reference in this context?

This is my code with some changes.. even though i dnt think they made a difference..
but part of my project is to learn something new... but i dnt understand this...

its still giving a exception in thread and nullpointer exception

import javax.swing.*;
import java.awt.*;
import java.awt.print.PrinterJob;
import java.awt.Frame.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;

public class OptionWindow extends JFrame implements ActionListener
{
	private JLabel factoryprofitL,grossfactL,salesL,returninL,srtiL, openingstockL,purchL, retunoutL, carriageinL, closingstockL,costofgoodssoldL,grossprofitL,elecL,telephoneL,carriageoutwardL,waterL,expensetotalL,netproflossL;
	private JLabel headingL,expensesheadingL;
	private JTextField grossfactTF,factoryprofitTF,headingTF,salesTF,returninTF,srtiTF, openingstockTF,purchTF, retunoutTF, carriageinTF, closingstockTF,costofgoodssoldTF,grossprofitTF,elecTF,telephoneTF,carriageoutwardTF,waterTF,expensetotalTF,netproflossTF;

	private CalculateButtonHandlerTpl cbtHandler;
	private ExitButtonHandlerTpl ebtHandler;
	private JButton exitTB, calculateTB;

	private JButton printB;
private PrintButtonHandler pbHandler;

/**
private Component componentToPrint;
private double cHeight;


JFrame frameToPrint;
    JTextArea text;
**/

private JFrame frameToPrint;

    public OptionWindow()
    {



		    setTitle("Trading Profit and Loss Account");

			salesL=new JLabel("Sales: \n",SwingConstants.RIGHT);
			returninL=new JLabel("Return Inwards: \n",SwingConstants.RIGHT);
			headingL=new JLabel("Cost of Goods Sold\n\n",SwingConstants.RIGHT);
			openingstockL=new JLabel("Opening Stock: \n",SwingConstants.RIGHT);
			factoryprofitL=new JLabel("Factory Profit/Loss: \n",SwingConstants.RIGHT);
			retunoutL=new JLabel("Return Outwards: \n",SwingConstants.RIGHT);
			carriageinL=new JLabel("Carriage Inwards: \n",SwingConstants.RIGHT);
			closingstockL=new JLabel("Closing Stock: \n",SwingConstants.RIGHT);

			grossprofitL=new JLabel("Gross Profit/Loss: \n",SwingConstants.RIGHT);
			expensesheadingL=new JLabel("Expenses\n",SwingConstants.RIGHT);
			elecL=new JLabel("Electricity: \n",SwingConstants.RIGHT);
			telephoneL=new JLabel("Telephone: \n",SwingConstants.RIGHT);
			carriageoutwardL=new JLabel("Carriage outwards: ",SwingConstants.RIGHT);
			waterL=new JLabel("Water: \n",SwingConstants.RIGHT);
			expensetotalL=new JLabel("Total expenses:\n",SwingConstants.RIGHT);
			netproflossL=new JLabel("Net Profit/Loss: \n",SwingConstants.RIGHT);

salesTF=new JTextField(10);
returninTF=new JTextField(10);
srtiTF=new JTextField(10);
//headingTF=new JTextField(10);
openingstockTF=new JTextField(10);
factoryprofitTF=new JTextField(10);
retunoutTF=new JTextField(10);
carriageinTF=new JTextField(10);
closingstockTF=new JTextField(10);
costofgoodssoldTF=new JTextField(10);
grossprofitTF=new JTextField(10);

elecTF=new JTextField(10);
telephoneTF=new JTextField(10);
carriageoutwardTF=new JTextField(10);
waterTF=new JTextField(10);
expensetotalTF=new JTextField(10);
netproflossTF=new JTextField(10);

		calculateTB=new JButton("Calculate");
		cbtHandler=new CalculateButtonHandlerTpl();
		calculateTB.addActionListener(cbtHandler);


		exitTB=new JButton("Exit");
		ebtHandler=new ExitButtonHandlerTpl();
		exitTB.addActionListener(ebtHandler);

printB=new JButton("Print");
pbHandler=new PrintButtonHandler();
printB.addActionListener(pbHandler);

		setTitle("Trading profit and loss account");

		Container pane=getContentPane();
		Container con=this.getContentPane();
		con.setBackground(Color.white);

		//pane.setLayout(new GridLayout(28,1));
con.setLayout(null);
con.setLayout(null );

		pane.add(salesL);
		pane.add(salesTF);
		salesL.setLocation(1,2);
    	salesL.setSize(190,25);
    	add(salesL);
        salesTF.setLocation(420,2);
    	salesTF.setSize(150,25);
    	add(salesTF);
		pane.add(returninL);
		pane.add(returninTF);
		returninL.setLocation(1,30);
		returninL.setSize(190,30);
		add(returninL);
		returninTF.setLocation(420,30);
		returninTF.setSize(150,25);
    	add(returninTF);
		pane.add(srtiTF);
		srtiTF.setLocation(420,58);
		srtiTF.setSize(150,25);
    	add(srtiTF);
    	srtiTF.setBorder(BorderFactory.createLoweredBevelBorder());
    srtiTF.setEditable(false);
		pane.add(headingL);
		headingL.setLocation(1,86);
		headingL.setSize(150,30);
		add(headingL);

		pane.add(openingstockL);
		pane.add(openingstockTF);
		openingstockL.setLocation(1,114);
		openingstockL.setSize(190,30);
		add(openingstockL);
		openingstockTF.setLocation(250,114);
		openingstockTF.setSize(150,25);
    	add(openingstockTF);

		pane.add(factoryprofitL);
		pane.add(factoryprofitTF);
		factoryprofitL.setLocation(1,142);
		factoryprofitL.setSize(190,30);
		add(factoryprofitL);
		factoryprofitTF.setLocation(250,142);
		factoryprofitTF.setSize(150,25);
    	add(factoryprofitTF);

		pane.add(retunoutL);
		pane.add(retunoutTF);
		retunoutL.setLocation(1,170);
		retunoutL.setSize(190,30);
		add(retunoutL);
		retunoutTF.setLocation(250,170);
		retunoutTF.setSize(150,25);
    	add(retunoutTF);

		pane.add(carriageinL);
		pane.add(carriageinTF);
		carriageinL.setLocation(1,198);
		carriageinL.setSize(190,30);
		add(carriageinL);
		carriageinTF.setLocation(250,198);
		carriageinTF.setSize(150,25);
    	add(carriageinTF);

		pane.add(closingstockL);
		pane.add(closingstockTF);
		closingstockL.setLocation(1,226);
				closingstockL.setSize(190,30);
				add(closingstockL);
				closingstockTF.setLocation(250,226);
				closingstockTF.setSize(150,25);
    	add(closingstockTF);

		pane.add(costofgoodssoldTF);
		costofgoodssoldTF.setLocation(420,254);
		costofgoodssoldTF.setSize(150,25);
    	add(costofgoodssoldTF);
    	costofgoodssoldTF.setBorder(BorderFactory.createLoweredBevelBorder());
		    costofgoodssoldTF.setEditable(false);

		pane.add(grossprofitL);
		pane.add(grossprofitTF);
		grossprofitL.setLocation(1,282);
		grossprofitL.setSize(190,30);
		add(grossprofitL);
		grossprofitTF.setLocation(420,282);
		grossprofitTF.setSize(150,25);
    	add(grossprofitTF);
    	grossprofitTF.setBorder(BorderFactory.createLoweredBevelBorder());
		    grossprofitTF.setEditable(false);


		pane.add(expensesheadingL);
		expensesheadingL.setLocation(1,310);
		expensesheadingL.setSize(150,30);
		add(expensesheadingL);

		pane.add(elecL);
		pane.add(elecTF);
		elecL.setLocation(1,338);
		elecL.setSize(190,30);
		add(elecL);
		elecTF.setLocation(250,338);
		elecTF.setSize(150,25);
    	add(elecTF);

		pane.add(telephoneL);
		pane.add(telephoneTF);
		telephoneL.setLocation(1,366);
		telephoneL.setSize(190,30);
		add(telephoneL);
		telephoneTF.setLocation(250,366);
		telephoneTF.setSize(150,25);
    	add(telephoneTF);

		pane.add(carriageoutwardL);
		pane.add(carriageoutwardTF);
		carriageoutwardL.setLocation(1,394);
		carriageoutwardL.setSize(190,30);
		add(carriageoutwardL);
		carriageoutwardTF.setLocation(250,394);
		carriageoutwardTF.setSize(150,25);
    	add(carriageoutwardTF);

		pane.add(waterL);
		pane.add(waterTF);
		waterL.setLocation(1,422);
		waterL.setSize(190,30);
		add(waterL);
		waterTF.setLocation(250,422);
		waterTF.setSize(150,25);
    	add(waterTF);

		pane.add(expensetotalL);
		pane.add(expensetotalTF);
		expensetotalL.setLocation(1,460);
		expensetotalL.setSize(190,30);
		add(expensetotalL);
		expensetotalTF.setLocation(420,460);
		expensetotalTF.setSize(150,25);
		add(expensetotalTF);
		expensetotalTF.setBorder(BorderFactory.createLoweredBevelBorder());
		    expensetotalTF.setEditable(false);


		pane.add(netproflossL);
		pane.add(netproflossTF);
		netproflossL.setLocation(1,498);
		netproflossL.setSize(190,30);
		add(netproflossL);
		netproflossTF.setLocation(420,498);
		netproflossTF.setSize(150,25);
		add(netproflossTF);
		netproflossTF.setBorder(BorderFactory.createLoweredBevelBorder());
		netproflossTF.setEditable(false);

		pane.add(calculateTB);
		calculateTB.setLocation(420,533);
		calculateTB.setSize(150,30);
		add(calculateTB);


		pane.add(exitTB);
		exitTB.setLocation(420,570);
		exitTB.setSize(150,30);
		add(exitTB);

		pane.add(printB);
		printB.setLocation(120,570);
		printB.setSize(150,30);
		add(printB);
setSize(600,700);
		setVisible(true);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		}

		private class CalculateButtonHandlerTpl implements ActionListener
		{
			public void actionPerformed(ActionEvent e)
		{
			double sales=0.0;
			double o_stock=0.0;
			double p_s=0.0;
			double c_i=0.0;
			double c_stock=0.0;
			double r_o=0.0;
			double r_i=0.0;
			double cgs=0.0;
			double srti=0.0;
			double g_p=0.0;
			double factprof=0.0;
			double c_o=0.0;
			double n_p=0.0;
			double exp=0.0;
			double electricity=0.0;
			double telephone=0.0;
			double carriageout=0.0;
			double water=0.0;
		sales=Double.parseDouble(salesTF.getText());
		r_i=Double.parseDouble(returninTF.getText());
		o_stock=Double.parseDouble(openingstockTF.getText());
		r_o=Double.parseDouble(retunoutTF.getText());
		c_i=Double.parseDouble(carriageinTF.getText());
		c_stock=Double.parseDouble(closingstockTF.getText());
		factprof=Double.parseDouble(factoryprofitTF.getText());
		electricity=Double.parseDouble(elecTF.getText());
		telephone=Double.parseDouble(telephoneTF.getText());
		c_o=Double.parseDouble(carriageoutwardTF.getText());
		water=Double.parseDouble(waterTF.getText());


		srti=sales-r_i;
		cgs=o_stock+factprof-r_o+c_i-c_stock;
		g_p=srti-cgs;

		exp=telephone+electricity+c_o+water;
		n_p=g_p-exp;

		srtiTF.setText(" "+srti);
		costofgoodssoldTF.setText(" "+cgs);
		grossprofitTF.setText(" "+g_p);

		expensetotalTF.setText(" "+exp);
		netproflossTF.setText(" "+n_p);
			}
		}
private class PrintButtonHandler implements ActionListener, Printable
{
    public int print(Graphics gx, PageFormat pf, int page) throws PrinterException
    {
		    if (page>0){return NO_SUCH_PAGE;}

		    Graphics2D g = (Graphics2D)gx;
		    g.translate(pf.getImageableX(), pf.getImageableY());
		   frameToPrint.printAll(g);
		    return PAGE_EXISTS;
}
 public void frameToPrint(JFrame f)
 {
        Container con = frameToPrint;
 }
 public void actionPerformed(ActionEvent e)
    {

       PrinterJob job = PrinterJob.getPrinterJob();

	   job.setPrintable(this);

	   if (job.printDialog() == true)
	   {
	       try {job.print();} catch (PrinterException ex){
	   }
	   }
    }
}

		private class ExitButtonHandlerTpl implements ActionListener
		{
		public void actionPerformed(ActionEvent e)
		{
		System.exit(0);
		}
}
 public void actionPerformed(java.awt.event.ActionEvent e)
 {
 }

     public static void main(String[] args)
     {
         new OptionWindow();
 	}
}

Define your frameToPrint variable at the class level in your PrintButtonHandler . Create a constructor for it that takes the Container as a parameter and sets frameToPrint to that reference. Then when you create the PrintButtonHandler up in your code, use new PrintButtonHandler(this); to pass the reference of your current OptionWindow frame to the handler constructor.

private class PrintButtonHandler implements ActionListener, Printable {
   Container frameToPrint;

   public PrintButtonHandler(Container frame){
      this.frameToPrint = frame;
   }
...

Since your handler is an inner class, you could also use OptionWindow.this to reference the current enclosing instance. The print call would just be OptionWindow.this.printAll(g);

Edited 5 Years Ago by Ezzaral: n/a

It works!!
Thank you so much...

so basically it was the "this" reference that made the class understand what to print instead of "OptionWindow.printAll(g);"

once again thank you so much

this represents the current object instance. Using it in an inner class would represent the instance of that inner class, so it must be qualified with the name of the enclosing class if you want to refer to the enclosing instance.

This question has already been answered. Start a new discussion instead.