Print four JPanels of a JAVA SWING program on a single page using PrinterJob

0

Taking advantage of the fact that my JAVA SWING application, of which I want to make a report, (Print the data to have them in physical format) has a very accomplished and worked appearance that could be used by printing the JPanels that show the information of my program.

I have reached the point where I can print the 4 panels (JPanels) that make up my program, here I leave the code:

--------- This is the class that is responsible for printing the jpanel that contains all my data --------------

public class Imprimir_Documento implements Printable{
private JPanel ComponenteSwing;
private double escalaX,escalaY;
public Imprimir_Documento(JPanel contentPane, double x, double y) {
    ComponenteSwing=contentPane;
    escalaX = x;
    escalaY = y;
}
public void DocumentoAImprimir(JFrame texto,double escalax, double escalay){

}
public int print(Graphics g, PageFormat pf, int page){
    if (page > 0) {
    return NO_SUCH_PAGE;
}
Graphics2D g2d = (Graphics2D)g;
g2d.translate(pf.getImageableX(), pf.getImageableY());
g2d.scale(escalaX, escalaY);
ComponenteSwing.printAll(g2d);
return PAGE_EXISTS;
}

}

--------- Here I leave the call I make from my JFrame and what I send to print -------

 PrinterJob printJob = PrinterJob.getPrinterJob();
        Book libro = new Book();

        libro.append(new Imprimir_Documento(panel_1, 0.25, 0.75), printJob.defaultPage());
        libro.append(new Imprimir_Documento(panel_2, 0.25, 0.75), printJob.defaultPage());
        libro.append(new Imprimir_Documento(panel_3, 0.25, 0.75), printJob.defaultPage());
        libro.append(new Imprimir_Documento(panel_4, 0.25, 0.75), printJob.defaultPage());

        printJob.setPageable(libro);

        if (printJob.printDialog()) {
            try {
                printJob.print();
            } catch (Exception PrinterException) {
                javax.swing.JOptionPane.showMessageDialog(null, PrinterException.getMessage());
            }
        }
    }

With what I have done so far (what you see) I can print the 4 panels that I want but with one drawback, each JPanel is printed on a different sheet and what I want to achieve is to print the 4 JPanels in the same one page, one below the other.

This is the point that I can not get, if someone can contribute something that would be very helpful, thanks for the attention.

The exact question would be:

WHAT CHANGE I HAVE TO DO TO BE ABLE TO PRINT THE 4 JPANELS ON THE SAME LEAF? (in case they did not enter, it would follow on a second page).

    
asked by Jouk 11.04.2017 в 14:52
source

0 answers