Issue:
When capturing an image of a JTable, the table header does not appear in the resulting image.
原因:
The JTable header is not part of the hierarchy by the time the panel is painted to the image, as it has been removed when the JOptionPane was closed.
kleopatra's Solution:
camickr's Solution:
Updated Code:
... // Without having been shown, fake a all-ready p.addNotify(); // Manually size to preferred p.setSize(p.getPreferredSize()); // Validate to force recursive doLayout of children p.validate(); BufferedImage bi = new BufferedImage(p.getWidth(), p.getHeight(), BufferedImage.TYPE_INT_RGB); Graphics g = bi.createGraphics(); p.paint(g); g.dispose(); ...
Conclusion:
Both solutions effectively render the JTable header in the resulting image. kleopatra's solution utilizes core J2SE, while camickr's leverages the ScreenImage API for additional functionality. The choice depends on the specific requirements of the application.
The above is the detailed content of How to Capture the Table Header in a Rendered JTable Image?. For more information, please follow other related articles on the PHP Chinese website!