0

I have added a KeyListener to JTable; but when I double click on a table cell, the KeyListener stops working.

public class MyKeyListener extends KeyAdapter {

    @Override
    public void keyTyped(KeyEvent ke) {
        char i = ke.getKeyChar();
        int ib = ((int) i);
        if ((ib == 8)) {
            if (jt1.isEditing()) {
                jt1.getCellEditor().cancelCellEditing();
            }
        } else {
            // my code to do
        }
    }
}
trashgod
  • 203,806
  • 29
  • 246
  • 1,045

2 Answers2

3

Don't use a KeyListener; use a Key Binding. More examples are cited here.

Alternatively, implement a custom table cell editor, as shown in the tutorial.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • Maybe in this situation Key Binding is ok. But if you want to perform simple action on your table, for example on arrow keys, it's easy to use KeyListener. And solve all incoming problems by JTable.requestFocusInWindow(). I spend 1h on this Bindings thing and simple KeyListener solve my problem. – Dracontis Apr 17 '13 at 21:03
  • 1
    Thank you for explaining your down-vote. I empathize, but I must disagree. – trashgod Apr 18 '13 at 00:53
-1

use MouseListener ...

jt1.addMouseListener(new MouseAdapter(){ 
        public void mousePressed(MouseEvent evt)  
            {  
                if (evt.getClickCount() == 2)
                {
                    jt1.getCellEditor().cancelCellEditing();
                                }
                         }
                  });

try the same for adding keyListener... in clickcount..

Beau Grantham
  • 3,435
  • 5
  • 33
  • 43
Vishnu
  • 1
  • 1