2010-06-10 120 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));