扩展@trashgod 的答案:选项1 是完全错误的:-) TableModel 必须包含数据,仅此而已。在表格中呈现数据(事实上,在任何 Swing 的集合视图中)呈现数据是渲染器的专有工作。以合理的形式导出数据是 TransferHandler 的工作,最好使用与渲染器相同的字符串表示形式。
JXTable 使得在协作者之间共享字符串表示变得特别容易:生成文本内容的小硬币称为 StringValue,所有内部渲染器都使用它进行配置。配置后,该字符串将用于所有与字符串相关的扩展功能,例如搜索、排序、基于正则表达式的过滤和表的 api:
String text = table.getStringAt(row, column);
这允许自定义 TransferHandler 将其字符串构建基于:
/**
* A TableTransferable that uses JXTable string api to build
* the exported data.
*
* C&p from BasicTableUI, replaced toString with
* table.getStringAt(row, col)
*/
public static class XTableTransferHandler extends TransferHandler {
/**
* Create a Transferable to use as the source for a data transfer.
*
* @param c The component holding the data to be transfered. This
* argument is provided to enable sharing of TransferHandlers by
* multiple components.
* @return The representation of the data to be transfered.
*
*/
@Override
protected Transferable createTransferable(JComponent c) {
if (!(c instanceof JXTable))
return null;
JXTable table = (JXTable) c;
int[] rows;
int[] cols;
if (!table.getRowSelectionAllowed()
&& !table.getColumnSelectionAllowed()) {
return null;
}
if (!table.getRowSelectionAllowed()) {
int rowCount = table.getRowCount();
rows = new int[rowCount];
for (int counter = 0; counter < rowCount; counter++) {
rows[counter] = counter;
}
} else {
rows = table.getSelectedRows();
}
if (!table.getColumnSelectionAllowed()) {
int colCount = table.getColumnCount();
cols = new int[colCount];
for (int counter = 0; counter < colCount; counter++) {
cols[counter] = counter;
}
} else {
cols = table.getSelectedColumns();
}
if (rows == null || cols == null || rows.length == 0
|| cols.length == 0) {
return null;
}
StringBuffer plainBuf = new StringBuffer();
StringBuffer htmlBuf = new StringBuffer();
htmlBuf.append("<html>\n<body>\n<table>\n");
for (int row = 0; row < rows.length; row++) {
htmlBuf.append("<tr>\n");
for (int col = 0; col < cols.length; col++) {
// original:
// Object obj = table.getValueAt(rows[row], cols[col]);
// String val = ((obj == null) ? "" : obj.toString());
// replaced by JXTable api:
String val = table.getStringAt(row, col);
plainBuf.append(val + "\t");
htmlBuf.append(" <td>" + val + "</td>\n");
}
// we want a newline at the end of each line and not a tab
plainBuf.deleteCharAt(plainBuf.length() - 1).append("\n");
htmlBuf.append("</tr>\n");
}
// remove the last newline
plainBuf.deleteCharAt(plainBuf.length() - 1);
htmlBuf.append("</table>\n</body>\n</html>");
return new BasicTransferable(plainBuf.toString(),
htmlBuf.toString());
}
@Override
public int getSourceActions(JComponent c) {
return COPY;
}
}
使用示例:
DefaultTableModel model = new DefaultTableModel(
new String[]{"Action"}, 0);
JXTable table = new JXTable(model);
Object[] keys = table.getActionMap().allKeys();
for (Object key : keys) {
model.addRow(new Object[]{table.getActionMap().get(key)});
}
StringValue sv = new StringValue() {
@Override
public String getString(Object value) {
if (value instanceof Action) {
return (String) ((Action) value).getValue(Action.NAME);
}
return StringValues.TO_STRING.getString(value);
}
};
table.getColumn(0).setCellRenderer(new DefaultTableRenderer(sv));
table.setDragEnabled(true);
table.setTransferHandler(new XTableTransferHandler());