2014-02-15 123 views
0

我創建的JComboBox並使用以下代碼中使用它作爲一個表中的單元格編輯器對某一列:編輯焦點在一個JTable細胞使用的JComboBox作爲細胞編輯

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

我還允許用戶使用Tab鍵在表格中從單元格移動到單元格。我遇到的問題是當單元由於使用標籤而獲得焦點時,用戶應該能夠使用鍵盤開始編輯。除了使用JComboBox作爲單元格編輯器的列之外,這適用於所有情況。對於那一欄,用戶必須用鼠標點擊單元格一次,然後才能從鍵盤輸入。我希望用戶能夠在他/她使用Tab鍵之後開始鍵入。我將不勝感激任何幫助。謝謝。

回答

0

我找到了上述問題的解決方案。我發現下面的鏈接: http://www.java-forums.org/awt-swing/29040-programmatically-starting-cell-editing-jtable.html 的解決方案涉及通過寫changeselection方法修改表的聲明:

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(); 
     } 
    } 
};