简体   繁体   English

JTable - 按Tab键使单元格进入编辑模式

[英]JTable - Getting a cell into Edit mode on pressing Tab

This is possibly has a trivial solution, but I am at the end of my tether so I hope somebody can help out. 这可能有一个微不足道的解决方案,但我在我的系绳的最后,所以我希望有人可以提供帮助。

I use a JTable which has a custom renderer and a custom editor for a set of columns. 我使用一个JTable,它有一个自定义渲染器和一组列的自定义编辑器。
The renderer uses a JLabel component and the editor uses a JSpinner component. 渲染器使用JLabel组件,编辑器使用JSpinner组件。
Our users want to be able to enter values in a column, and then press TAB or ENTER to move to the next editable cell in the table. 我们的用户希望能够在列中输入值,然后按TAB或ENTER移动到表格中的下一个可编辑单元格。
If I understand correctly, this is the default behaviour for a JTable. 如果我理解正确,这是JTable的默认行为。

However, this doesn't seem to work correctly for me. 但是,这对我来说似乎不正常。 Until the user clicks on the cell, only the JLabel is displayed. 在用户单击单元格之前,仅显示JLabel。
The JSpinner (ie CellEditor) is only displayed when a user double clicks on the cell. 只有当用户双击单元格时才会显示JSpinner(即CellEditor)。 So, it looks like the cell is going into "edit" mode only on MouseEvents, but not when it has focus. 因此,看起来单元格仅在MouseEvents上进入“编辑”模式,但在有焦点时则不然。

How do I get the cell to go into edit mode as soon as it has focus? 如果单元格有焦点,如何让单元格进入编辑模式?

Thank you n00213f. 谢谢n00213f。 The thread and example from your post were helpful. 帖子中的帖子和示例很有帮助。 By overloading the changeSelection method in JTable as hinted to in the thread, JTable checks if a cell is editable every time the selection is changed. 通过在线程中暗示的JTable中重载changeSelection方法,JTable会在每次更改选择时检查单元是否可编辑。 If the cell is editable, it will show the CellEditor and transfer focus to the editor component. 如果单元格是可编辑的,它将显示CellEditor并将焦点转移到编辑器组件。

For completeness, here is my solution: 为了完整起见,这是我的解决方案:

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

You can achieve this programatically, you simply listen to the focus events on the cell, on focus and editing allowed, start editing. 您可以以编程方式实现此目的,只需在单元格上进行焦点事件,在焦点和编辑允许的情况下,开始编辑。

More on this thread and example 更多关于这个线程例子

Here's a code snippet that I put together for a project that I was working on. 这是我为我正在处理的项目整理的代码片段。 The code has been tested and verified for a table that has non-editable cells in the first and last column. 代码已针对在第一列和最后一列中具有不可编辑单元格的表进行测试和验证。 The class restricts tabbing to only the editable cells of the table. 该类将Tab键限制为仅表格的可编辑单元格。 It also supports shift-tabbing to tab in reverse. 它还支持反向切换到制表符。

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

} }

And here's how the class is used for your table: 以下是该类如何用于您的表:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM