I have a JTable set in SINGLE_SELECTION mode, i.e. the user can select only one row at a time. I am trying to override CTRL + C KeyListener to copy the entire table to the clipboard.
Currently, I have added KeyListener to JTable itself in my constructor:
public MyTable(AbstractTableModel model) { super(model); getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION); addKeyListener(new ExcelClipboardKeyAdapter(this)); }
KeyListener is as follows:
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) {
However, when I press CTRL + C , the keyreleased method keyreleased not called and does not print "here." The contents of the clipboard contains only the selected line.
Any ideas are welcome.
EDIT
Actually it sometimes works several times, then it stops working and again copies one line ... weird ...
source share