Reputation: 261
I'm using netbeans and I have the code to print a jpanel and it works fine. However, its output is that it prints the jpanel as an image. This is not ideal for me as it also prints a faded gray color in the background which is the size of the jpanel. I only want to print the text in the jpanel which consists of many jlabels. Is there a way to print the contents of a jpanel as text and not an image?
This is the code that I used to print the contents of my jpanel
PrinterJob job = PrinterJob.getPrinterJob();
job.setJobName("jPanel13");
job.setPrintable (new Printable() {
public int print(Graphics pg, PageFormat pf, int pageNum){
if (pageNum > 0){
return Printable.NO_SUCH_PAGE;
}
Graphics2D g2 = (Graphics2D) pg;
g2.translate(pf.getImageableX(), pf.getImageableY());
jPanel13.paint(g2);
return Printable.PAGE_EXISTS;
}
});
boolean ok = job.printDialog();
if (ok) {
try {
job.print();
} catch (PrinterException ex) {
}
This is a printscreen of the jpanel that I need to print. In my program, the text values of the jlabels can be changed by the user. The jlabels texts will then be printed to fill in the blank lines a form. The form will already have writing on it; it is only the blank lines that I will fill-up when printing. That is why it is important that the jlabels are positioned in that way.
https://i.sstatic.net/kPKWe.jpg
Upvotes: 1
Views: 5056
Reputation: 16364
If the JPanel
background is the only problem, you can call print()
on it instead of paint()
, and override printComponent()
to do nothing (the default just calls paintComponent()
).
Alternately, just set the background color to white while printing.
Upvotes: 3