2010-06-10 123 views
9

這可能有一個微不足道的解決方案,但我在我的繫繩的末尾,所以我希望有人可以幫忙。JTable - 按Tab鍵進入編輯模式

我使用了一個JTable,它有一組自定義渲染器和一組自定義編輯器。
渲染器使用JLabel組件,編輯器使用JSpinner組件。
我們的用戶希望能夠在列中輸入值,然後按TAB或ENTER移動到表格中的下一個可編輯單元格。
如果我理解正確,這是JTable的默認行爲。

但是,這似乎並不適合我。在用戶單擊單元格之前,只顯示JLabel。
JSpinner(即CellEditor)僅在用戶雙擊單元格時顯示。因此,它看起來像單元格只在MouseEvent上進入「編輯」模式,但是當它具有焦點時纔會進入「編輯」模式。

如何讓單元格一旦進入焦點就進入編輯模式?

回答

4

您可以通過編程實現這一功能,只需在單元格上監聽焦點事件,進行焦點和編輯,即可開始編輯。

更多關於此threadexample

18

謝謝n00213f。帖子中的帖子和示例很有幫助。通過在線程中將JTable中的012Select方法更改爲changeSelect,JTable會在每次更改選擇時檢查單元格是否可編輯。如果單元格是可編輯的,它將顯示CellEditor並將焦點傳遞給編輯器組件。

爲了完整起見,這裏是我的解決方案:

JTable myTable = new javax.swing.JTable() 
    { 
      public void changeSelection(final int row, final int column, boolean toggle, boolean extend) 
      { 
       super.changeSelection(row, column, toggle, extend); 
       myTable.editCellAt(row, column); 
       myTable.transferFocus(); 
      } 
    }; 
+0

很好聽的答案是有用的。 – n002213f 2010-06-10 18:14:13

2

下面是一個代碼片段,我放在一起的,我是工作的一個項目。該代碼已經過測試並驗證了在第一列和最後一列中具有不可編輯單元格的表格。該類將選項卡限制爲僅限表格的可編輯單元格。它也支持反向移動標籤到標籤。

public class JTableCellTabbing { 
/** 
* 
* Creates a new {@code JTableCellTabbing} object. 
* 
* 
*/ 
private JTableCellTabbing() {   
} 

/** 
* 
* Set Action Map for tabbing and shift-tabbing for the JTable 
* 
* 
* @param theTable - Jtable with NRows and MCols of cells 
* @param startRow - valid start row for tabbing [ 0 - (numRows-1) ] 
* @param numRows - Number of rows for tabbing 
* @param startCol - valid start col for tabbing [ 0 - (numCols-1) ] 
* @param numCols - Number of columns for tabbing 
*/ 
@SuppressWarnings("serial") 
static public void setTabMapping(final JTable theTable, final int startRow, final int numRows, final int startCol, final int numCols) { 
    if (theTable == null) { 
     throw new IllegalArgumentException("theTable is null"); 
    } 

    // Calculate last row and column for tabbing 
    final int endRow = startRow + (numRows - 1); 
    final int endCol = startCol + (numCols - 1); 

    // Check for valid range 
    if ((startRow > endRow) || (startCol > endCol)) { 
     throw new IllegalArgumentException("Table Size incorrect");    
    } 

    // Get Input and Action Map to set tabbing order on the JTable 
    InputMap im = theTable.getInputMap(JTable.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); 
    ActionMap am = theTable.getActionMap(); 

    // Get Tab Keystroke 
    KeyStroke tabKey = KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0);      
    am.put(im.get(tabKey), new AbstractAction() { 

     @Override 
     public void actionPerformed(ActionEvent e) { 
      int row = theTable.getSelectedRow(); 
      int col = theTable.getSelectedColumn(); 

      col++; 

      // Move to next row and left column 
      if (col > endCol) { 
       col = startCol; 
       row++; 
      } 

      // Move to top row 
      if (row > endRow) { 
       row = startRow; 
      } 

      // Move cell selection 
      theTable.changeSelection(row, col, false, false); 
     }    
    }); 

    // Get Shift tab Keystroke 
    KeyStroke shiftTab = 
     KeyStroke.getKeyStroke(KeyEvent.VK_TAB, java.awt.event.InputEvent.SHIFT_DOWN_MASK);      
    am.put(im.get(shiftTab), new AbstractAction() { 

     @Override 
     public void actionPerformed(ActionEvent e) { 
      int row = theTable.getSelectedRow(); 
      int col = theTable.getSelectedColumn(); 

      col--; 

      // Move to top right cell 
      if (col < startCol) { 
       col = endCol; 
       row--; 
      } 

      // Move to bottom row 
      if (row < startRow) { 
       row = endRow; 
      } 

      // Move cell selection 
      theTable.changeSelection(row, col, false, false); 
     }    
    });      
} 

}

而這裏的類是如何使用您的表:

JTable myTable = new JTable(); 
// Set up table attributes.... 
JTableCellTabbing.setTabMapping(myTable, 0, NUM_ROWS, 1, (NUM_COLS-1));