How to correctly register copy cell value action on press Ctrl + C?

已计划

I tried to use common method to register Ctrl + C action for all my tables in plugin:

public static void registerCtrlCTableAction(JTable table) {

final KeyStroke stroke = KeyStroke.getKeyStroke(KeyEvent.VK_C, InputEvent.CTRL_DOWN_MASK, false);
table.registerKeyboardAction(new ActionListener() {
  @Override
  public void actionPerformed(ActionEvent e) {
       int col = table.getSelectedColumn();
       int row = table.getSelectedRow();
      Debug.log("Ctrl+C: " + col + " " + row);
      if (col != -1 && row != -1) {
         Object value = table.getValueAt(row, col);
         String data = value != null ? value.toString() : "";
         StringSelection stringSelection = new StringSelection(data);
         Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
        clipboard.setContents(stringSelection, null);
    }
}
}, "Copy",stroke, JComponent.WHEN_FOCUSED);


}

But this doesn't work. Can't find in documentation how to implement this correctly :(

0

请先登录再写评论。