i've created a jframe with a jpanel with certain things as you can see in the attachment (page1.java)
..............can anyone tell how to print that jpanel(not the whole jframe) and all its contents ........

Recommended Answers

All 6 Replies

I'm not going to download your zip and look through it entirely. please post your relevant code and be a bit more specific as to what you want..
do you want the JPanel to be printed, or the contents of the JPanel?

ya i want the JPanel and the contents of the JPanel to be printed automatically........

jpanel.setText ("Text Here");

You may need to revalidate.

What do you mean by "printing"?
Do you want a copy of the GUI component and contents written/printed to paper?

contents of the jpanel to be printed on to a paper through my printer............

The trick is to get the graphics object for the printing output and then call the components paintComponent method with that object. It will draw itself on the graphics context.

Here's a sample print out to a .PS file using the following:

JFrame frameToPrint;

    public int print(Graphics g, PageFormat pf, int page) throws
                                                        PrinterException {

        if (page > 0) {
            return NO_SUCH_PAGE;
        }

        Graphics2D g2d = (Graphics2D)g;
        g2d.translate(pf.getImageableX(), pf.getImageableY());

        /* Now print the window and its visible contents */
        frameToPrint.printAll(g);

        return PAGE_EXISTS;
    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.