Question

I created a JComboBox and used it as the cell editor for a certain column in a table using the following code:

iledgerEditortxt = new JComboBox(buildComboBoxmodel("SELECT ledger_name FROM ledgers"));
AutoCompleteDecorator.decorate(iledgerEditortxt);
TableColumn ledgerColumn = itemsMaintainTable.getColumnModel().getColumn(2);
ledgerColumn.setCellEditor(new ComboBoxCellEditor(iledgerEditortxt));    

I have also allowed the user to move from cell to cell in the table using the tab key. The problem that I am having is that when a cell gains focus due to the use of tab the user should be able to start editing using the keyboard. This works in all cases except for the column that uses the JComboBox as the cell editor. For that column, the user has to click on the cell once with his mouse and only then can he type from the keyboard.I want the user to be able to start typing after he/she uses the tab key. I would appreciate any help. Thanks.

Was it helpful?

Solution

I found a solution to the problem that I stated above. I found it on the following link: http://www.java-forums.org/awt-swing/29040-programmatically-starting-cell-editing-jtable.html The solution involved modifying the declaration of the table by writing the changeselection method:

JTable table = new JTable(data, columnNames) {
    public void changeSelection(int row, int column, boolean toggle, boolean extend) {
        super.changeSelection(row, column, toggle, extend);
        if (editCellAt(row, column))
        {
            Component editor = getEditorComponent();
            editor.requestFocusInWindow();
        }
    }
};
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top