JTable:覆盖CTRL + C行为 [英] JTable: override CTRL+C behaviour

查看:128
本文介绍了JTable:覆盖CTRL + C行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在SINGLE_SELECTION模式下设置了一个JTable,即用户一次只能选择一行. 我试图覆盖 CTRL + C KeyListener,以便它将整个表复制到剪贴板.

I have a JTable set on SINGLE_SELECTION mode, i.e. the user can only select one row at a time. I am trying to override the CTRL+C KeyListener so that it will copy the whole table to the clipboard.

此刻,我已经在JTable的构造函数中为其添加了一个KeyListener:

At the moment, I have added a KeyListener to the JTable itself in its constructor:

public MyTable(AbstractTableModel model) {
    super(model);
    getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    addKeyListener(new ExcelClipboardKeyAdapter(this));
}

KeyListener看起来像这样:

The KeyListener looks like this:

public class ExcelClipboardKeyAdapter extends KeyAdapter {

    private static final String LINE_BREAK = System.lineSeparator();
    private static final String CELL_BREAK = "\t";
    private static final Clipboard CLIPBOARD = Toolkit.getDefaultToolkit().getSystemClipboard();
    private final JTable table;

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

    @Override
    public void keyReleased(KeyEvent event) {
        if (event.isControlDown()) {
            if (event.getKeyCode() == KeyEvent.VK_C) { // Copy                        
                copyToClipboard();
                System.out.println("here");
            }
        }
    }

    private void copyToClipboard() {
        int numCols = table.getColumnCount();
        int numRows = table.getRowCount();
        StringBuilder excelStr = new StringBuilder();
        for (int i = 0; i < numRows; i++) {
            for (int j = 0; j < numCols; j++) {
                excelStr.append(escape(table.getValueAt(i, j)));
                if (j < numCols - 1) {
                    excelStr.append(CELL_BREAK);
                }
            }
            excelStr.append(LINE_BREAK);
        }

        StringSelection sel = new StringSelection(excelStr.toString());
        CLIPBOARD.setContents(sel, sel);
    }

    private String escape(Object cell) {
        return (cell == null? "" : cell.toString().replace(LINE_BREAK, " ").replace(CELL_BREAK, " "));
    }
}

但是,当我按 CTRL + C 时,不会调用keyreleased方法,并且不会在此处"打印.剪贴板的内容仅包含选定的行.

However, when I press CTRL+C, the keyreleased method is not called and does not print "here". The content of the clipboard only contains the selected line.

任何想法都将受到欢迎.

Any ideas would be welcome.

编辑

实际上,有时它会工作几次,然后停止工作,然后再次复制一行...很奇怪...

Actually it sometimes works a few times then it stops working and copies one row again... weird...

推荐答案

将我的评论移入答案:

实现一个自定义TransferHandler,该自定义TransferHandler创建"excel-transferable"并在表中使用它(dragEnabled == true)-适用于目标OS的键绑定-然后自动进行接线

implement a custom TransferHandler which creates the "excel-transferable" and use that in the table (with dragEnabled == true) - the keybinding as appropriate for the target OS - then is automatically wired

这篇关于JTable:覆盖CTRL + C行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆