JTable的默认TransferHandler以制表符分隔的格式导出数据。我正在尝试创建自己的TransferHandler,该数据将数据导出为HTML表,但是目前使用该工具时,没有任何内容被复制到剪贴板。如何添加此功能?

当前,我正在使用table.setTransferHandler(new TableTransferHandler());将以下TransferHandler添加到我的表中:

public class TableTransferHandler extends TransferHandler {
    final static Logger logger = LoggerFactory.getLogger("TableTransferHandler");

    @Override
    protected Transferable createTransferable(JComponent comp) {
        JTable table = (JTable) comp;
        logger.debug("creating table selection transferable");
        return new TableSelection(table);
    }

}


TableSelection可转让:

public class TableSelection implements Transferable {
    final static Logger logger = LoggerFactory.getLogger("TableSelection");

    private static DataFlavor HTML_TEXT;

    private static final List<DataFlavor> flavors = new ArrayList<DataFlavor>();

    private JTable table;

    public TableSelection(JTable table) {
        this.table = table;
    }

    @Override
    public boolean isDataFlavorSupported(DataFlavor flavor) {
        return flavors.contains(flavor);
    }

    @Override
    public DataFlavor[] getTransferDataFlavors() {
        return flavors.toArray(new DataFlavor[0]);
    }

    @Override
    public Object getTransferData(DataFlavor flavor) throws IOException, UnsupportedFlavorException {
        if (flavor.equals(HTML_TEXT)) {
            StringBuilder sb = new StringBuilder("<table>");
            int[] rows = table.getSelectedRows();
            int[] cols = table.getSelectedColumns();

            for (int row : rows) {
                int col_index = 0;
                sb.append("<tr><td>");
                for (int col : cols) {
                    Object value = table.getValueAt(row, col);
                    if (value != null)
                        sb.append(value.toString());

                    if (col_index + 1 < cols.length)
                        sb.append("</td><td>");
                }
                sb.append("</td></tr>\n");
            }
            sb.append("</table>");

            return sb.toString();
        }
        else {
            throw new UnsupportedFlavorException(flavor);
        }
    }

    static {

        try {
            HTML_TEXT = new DataFlavor("text/html;class=java.lang.String");
            flavors.add(HTML_TEXT);
        }
        catch (ClassNotFoundException e) {
            logger.error("Couldn't create HTML_TEXT flavor", e);
        }
    }
}

最佳答案

只有一件丢失了。您需要实现TransferHandler.getSourceActions,如下所示:

public class TableTransferHandler extends TransferHandler {
    ...
    @Override
    public int getSourceActions(JComponent c) {
        return COPY;
    }
}


编辑:这在Mac OS X上有效。但是在Windows上,可以粘贴到记事本或写字板,但不能用于OpenOffice calc。经过一番调查,我认为Java AWT和OpenOffice都有故障:


Java产生以下HTML剪贴板格式:


  版本:0.9
  开始HTML:-1
  EndHTML:-1
  ...
  
  这两个-1是错误的。它们应该是一些正偏移量。有一个know JDK bug与此处的内容非常接近。



另一方面,OpenOffice无法处理这种部分正确的剪贴板格式,而记事本或写字板则可以。如果在OpenOffice bugzilla数据库中搜索“粘贴html计算器”,则为7300+ (!) open bugs

07-28 13:21