Diego Alejandro
Diego Alejandro

Reputation: 1

Printing two JPanels in a same page

I've been tried to print 2 JPanels in the same page, each jpanel fits perfectly in a half page.

What I do is wrap the jpanels in another jpanel that implements Printable.

The problem is that, only print correctly the first (top) jpanel,

the rest of the page is filled with a gray square

This is the code of the Wrapper Jpanel

public int print(Graphics graphics, PageFormat pageFormat, int pageIndex)
            throws PrinterException
    {

        if (pageIndex == 0)
        {
            Graphics2D g2 = (Graphics2D) graphics;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                    RenderingHints.VALUE_ANTIALIAS_ON);             
            g2.scale(.485, .473);    
            paint(graphics);    
            g2.setColor(Color.white);
             g2.fillRect(0, 0, 1, 1);

            return Printable.PAGE_EXISTS;
        } else
        {
            return Printable.NO_SUCH_PAGE;
        }

    }

This the calling code

PrinterJob pj = PrinterJob.getPrinterJob();
        pj.setJobName("Factura Procter & Gamble");

        if (pj.printDialog())
        {
            try
            {
                for (int i = 0; i < 1; i++)
                {
                    pj.setPrintable(listaPaneles.get(i));
                    pj.print();

                }
            } catch (PrinterException e)
            {
                System.out.println(e);
            }
        }

please help me, Thanks.

Upvotes: 0

Views: 531

Answers (1)

Destroyica
Destroyica

Reputation: 4267

Firstly, I think that you are not printing them all

instead of

for (int i = 0; i < 1; i++)
{
    pj.setPrintable(listaPaneles.get(i));
    pj.print();
}

try

for (JPanel jPanel : listaPaneles)
{
    pj.setPrintable(jPanel);
    pj.print();
}

Secondly, maybe you need to wrap your two panels inside another Printable container and print this one only.

Upvotes: 1

Related Questions