Can any one help in how to print a JFrame
on A4 Size paper in java. I have created a JFrame
for printing a bill, but it is not working.
I tried to use Printable
interface but it doesn't seems to work for me.
Asked
Active
Viewed 226 times
-3

Gaurav
- 3,615
- 2
- 27
- 50

chirag jadhav
- 11
- 2
-
2Something like [this](http://stackoverflow.com/questions/22241711/setting-print-size-of-a-jlabel-and-put-a-jradiobutton-on-the-print/22242658#22242658) or [this](http://stackoverflow.com/questions/22241711/setting-print-size-of-a-jlabel-and-put-a-jradiobutton-on-the-print/22244116#22244116) or [this](http://stackoverflow.com/questions/22058738/print-jlabels-icon-in-a-printer-using-a-button/22059079#22059079) or [this](http://stackoverflow.com/questions/12764634/printing-a-jframe-and-its-components/12765916#12765916) – MadProgrammer Feb 17 '15 at 04:20
-
1or [this](http://stackoverflow.com/questions/17904518/fit-scale-jcomponent-to-page-being-printed/17961911#17961911)? – MadProgrammer Feb 17 '15 at 04:23
-
Or use something like Jasper Reports – MadProgrammer Feb 17 '15 at 04:31
-
Or you could use `JTable`, for [exmaple](http://stackoverflow.com/questions/26580954/how-to-print-selected-rows-jtable/26581137#26581137) or [example](http://stackoverflow.com/questions/24977871/printing-selected-component-of-jframe-to-printer/24978159#24978159) – MadProgrammer Feb 17 '15 at 05:03
1 Answers
0
If you want to print just text you can use
JTextPane jtp = new JTextPane();
jtp.setBackground(Color.white);
jtp.setText(text);//Set text to print.
try {
jtp.print(null, null, true, null, null, true);
} catch (java.awt.print.PrinterException ex) {
ex.printStackTrace();
}
If you actually want to print the entire JFrame you could try taking a screen shot of the user's screen and crop it to the JFrame or do this and then use something like this to print the image.
-
Or simply call `print` on the `JFrame`, but the problem there is you would need to resize the frame so as it make sure it could fit within the boundaries of A4 sheet of paper, which seems to be, at least, half the question here – MadProgrammer Feb 17 '15 at 04:28
-
Okay... then find how many pixels can fit on the A4 paper then take the longer side of the the JFrame and re-size to fit then resize the other side proportionality. – Forseth11 Feb 17 '15 at 04:36
-