Search code examples
javaswingjtablefocusmanager

JTable cell does not fire key events when editing is forced on the cell through editCellAt(row,col)


Below is the snippet of my code. I have a JTable. I have extended the DefaultCellEditor to create my own editor.I have key listeners and mouse listeners added for the cells.I have a JButton as well. When i click on the JButton, i want the first cell in the JTable to enter edit mode.. For this i have used requestFocus and editCellAt(0,0). I have put this code in the actionperformed.

public void actionPerformed(ActionEvent e)
{
     System.out.println("action performed");

     if(e.getSource().equals(btn))
     {
    oTable.requestFocus();
    oTable.setRowSelectionInterval(0, 0);
    oTable.editCellAt(0, 0);
     }
}

This places the cursor in the first cell. But when i type anything, the key events are not fired! Note: if i use my mouse to click on the cell and then type, it does fire key events. But I don't want to do this extra click.


Solution

  • Seems to work for me. Maybe you can try the following:

    table.editCellAt(0, 0);
    table.getEditorComponent().requestFocus();