Mengapakah pengepala JTable tidak dipaparkan dalam imej?
Pengepala JTable hilang daripada PNG kerana pengepala itu bukan lagi sebahagian daripada hierarki apabila panel dilukis pada imej. Apabila anak tetingkap pilihan ditutup, pengepala dialih keluar daripada hierarki. Untuk memasukkannya semula, gunakan coretan ini:
table.addNotify(); p.doLayout(); BufferedImage bi = new BufferedImage(p.getWidth() + 100, p.getHeight() + 100, BufferedImage.TYPE_INT_RGB); Graphics g = bi.createGraphics(); p.paint(g); g.dispose();
Berikut ialah coretan kod yang menangkap data JTable dan menyimpannya sebagai imej PNG, dan juga termasuk penyelesaian daripada camickr dan kleopatra:
import javax.swing.*; import java.awt.*; import java.awt.image.BufferedImage; import javax.imageio.ImageIO; import java.io.File; class TableImage { String[] columns = {"Name", "Age", "GPA", "Pass"}; /** Any resemblance to persons living or dead is purely incidental. */ Object[][] data = { {"André", new Integer(23), new Double(47.64), new Boolean(false)}, {"Jeanie", new Integer(23), new Double(84.81), new Boolean(true)}, {"Roberto", new Integer(22), new Double(78.23), new Boolean(true)} }; TableImage() { } public JTable getTable() { JTable table = new JTable(data, columns); table.setGridColor(new Color(115,52,158)); table.setRowMargin(5); table.setShowGrid(true); return table; } /** Method courtesy of camickr. https://stackoverflow.com/questions/7369814/why-does-the-jtable-header-not-appear-in-the-image/7375655#7375655 Requires ScreenImage class available from.. http://tips4java.wordpress.com/2008/10/13/screen-image/ */ public BufferedImage getImage1(JTable table) { JScrollPane scroll = new JScrollPane(table); scroll.setColumnHeaderView(table.getTableHeader()); table.setPreferredScrollableViewportSize(table.getPreferredSize()); JPanel p = new JPanel(new BorderLayout()); p.add(scroll, BorderLayout.CENTER); BufferedImage bi = ScreenImage.createImage(p); return bi; } /** Method courtesy of kleopatra. https://stackoverflow.com/questions/7369814/why-does-the-jtable-header-not-appear-in-the-image/7372045#7372045 */ public BufferedImage getImage2(JTable table) { JScrollPane scroll = new JScrollPane(table); table.setPreferredScrollableViewportSize(table.getPreferredSize()); JPanel p = new JPanel(new BorderLayout()); p.add(scroll, BorderLayout.CENTER); // without having been shown, fake a all-ready p.addNotify(); // manually size to pref 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(); return bi; } public void writeImage(BufferedImage image, String name) throws Exception { ImageIO.write(image,"png",new File(name + ".png")); } public static void main(String[] args) throws Exception { UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"); TableImage ti = new TableImage(); JTable table; BufferedImage bi; table = ti.getTable(); bi = ti.getImage1(table); ti.writeImage(bi, "1"); JOptionPane.showMessageDialog(null, new JLabel(new ImageIcon(bi))); table = ti.getTable(); bi = ti.getImage2(table); ti.writeImage(bi, "2"); JOptionPane.showMessageDialog(null, new JLabel(new ImageIcon(bi))); } }
Atas ialah kandungan terperinci Mengapakah pengepala JTable hilang apabila menangkap jadual sebagai imej?. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!