How to capture a Swing GUI element?

Is it possible to capture only a Swing GUI element (e.g. a JPanel containing some JTextFields)?

+2
source share
2 answers

The following method should capture a Swing GUI element:

public void captureComponent(JComponent component, File imageFile) throws IOException {
    BufferedImage bufImage = new BufferedImage(component.getSize().width, component.getSize().height, BufferedImage.TYPE_INT_RGB);  
    component.paint(bufImage.createGraphics());   
    imageFile.createNewFile();  
    ImageIO.write(bufImage, "jpeg", imageFile);
}
+3
source

The screen image will do what you want.

+2
source

All Articles