我一直在致力于从API提取数据并将其放入JTable的项目。当用数据填充JTable时,我希望它保存表中所有内容的图像,包括标题。
问题在于数据似乎只能保存一次。如果我处理表格(即:使用其他JComponent删除行)并尝试再次保存图像,则会出现错误,因为高度/宽度无法设置为0。
以下是我的一些代码,用于向表中添加数据:
missionTable = new JTable(info, headers);
for (int i = 0; i < missionTable.getColumnCount(); i++) {
missionTable.getColumnModel().getColumn(i).setCellRenderer(new RarityRenderer());
}
int maxSize = 0;
JTextArea label = null;
// Resize rows to fit table values
for (int i = 0; i < info.length; i++) {
maxSize = 0;
if (info[i] != null && info[i][2] != null && info[i][3] != null) {
label = new JTextArea(info[i][2]);
label.setBounds(0, 0, 95, 20);
if (label.getPreferredSize().height > maxSize)
maxSize = label.getPreferredSize().height;
label = new JTextArea(info[i][3]);
label.setBounds(0, 0, 320, 20);
label.setLineWrap(true);
label.setWrapStyleWord(true);
if (label.getPreferredSize().height > maxSize)
maxSize = label.getPreferredSize().height;
}
if (maxSize > 0)
missionTable.setRowHeight(i, maxSize > 40 ? 52 : 40);
}
label = null;
missionScrollPane.setViewportView(missionTable);
System.out.println("H:" + missionTable.getHeight() + " W:" + missionTable.getWidth());
saveTable(missionTable, "./test.jpg");// Line 904 from stacktrace
这是saveTable(JTable,String)方法:
public void saveTable(JTable table, String filename) {
try {
int ww = Math.max(table.getWidth(), table.getTableHeader().getWidth());
int hh = table.getHeight() + table.getTableHeader().getHeight();
BufferedImage bi = new BufferedImage(ww, hh, BufferedImage.TYPE_INT_RGB);// Line 980 from stacktrace
Graphics2D g2 = bi.createGraphics();
table.getTableHeader().paint(g2);
g2.translate(0, table.getTableHeader().getHeight());
table.paint(g2);
g2.dispose();
OutputStream out = new FileOutputStream(filename);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(bi);
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
连同输出+ stacktrace:
H:0 W:0
java.lang.IllegalArgumentException: Width (0) and height (0) cannot be <= 0
at java.awt.image.DirectColorModel.createCompatibleWritableRaster(Unknown Source)
at java.awt.image.BufferedImage.<init>(Unknown Source)
at stuff.game.ux.StormUI.saveComponentAsJPEG(StormUI.java:980)
at stuff.game.ux.StormUI.refreshCurrentTable(StormUI.java:904)
at stuff.game.ux.StormUI.changeFilter(StormUI.java:720)
at stuff.game.ux.StormUI$9.mouseClicked(StormUI.java:425)
at java.awt.AWTEventMulticaster.mouseClicked(Unknown Source)
at java.awt.AWTEventMulticaster.mouseClicked(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
老实说,我已经尝试不了了,手动设置高度/宽度只能创建纯黑色图像。