0

How is it possible to store a JPanel as a jpeg image the solutions I could search takes a snapshot of the display, this does not capture all the contents of the JPanel as part of it is hidden. Is there any way of getting the entire image in jpeg.

This solution available here is not working fine at all solution

code I am currently using to take snapshot of JPanel

if (panelx == null) {
            jButton3.setText("Generate Diagram");
        } else {
            jButton3.setText("Generate Diagram");
            jTextPane1
                    .setText(Messages.getString("statement2")); //$NON-NLS-1$
            JFrame win = (JFrame) SwingUtilities
                    .getWindowAncestor(panelx.panelx);
            Dimension size = win.getSize();
            // BufferedImage image = new BufferedImage(size.width, size.height,
            // BufferedImage.TYPE_INT_RGB);
            BufferedImage image = (BufferedImage) win.createImage(size.width,
                    size.height);
            Graphics g = image.getGraphics();
            win.paint(g);
            g.dispose();
            try {

                ImageIO.write(
                        image,
                        Messages.getString("Statement38"), new File(Messages.getString("statement5"))); //$NON-NLS-1$ //$NON-NLS-2$
            } catch (IOException e) {
                System.out.println("Image cannot be created");
            }
Community
  • 1
  • 1
Sanyam Goel
  • 2,138
  • 22
  • 40

1 Answers1

0

You don't explain why the example doesn't work.

Why are you using the window not the panel? If you were using the example answer you should refer to the panel within the scrollpane.

You're calling paint on the whole JFrame rather than just the panel you are interested in.

plasma147
  • 2,191
  • 21
  • 35