【问题标题】:JTable - Getting a cell into Edit mode on pressing TabJTable - 在按 Tab 时使单元格进入编辑模式
【发布时间】:2019-01-31 17:15:28
【问题描述】:

这可能有一个简单的解决方案,但我已经束手无策了,所以我希望有人能帮忙。

我使用一个 JTable,它有一个自定义渲染器和一组列的自定义编辑器。
渲染器使用 JLabel 组件,编辑器使用 JSpinner 组件。
我们的用户希望能够在列中输入值,然后按 TAB 或 ENTER 键移动到表中的下一个可编辑单元格。
如果我理解正确,这是 JTable 的默认行为。

但是,这对我来说似乎无法正常工作。在用户单击单元格之前,只显示 JLabel。
JSpinner(即 CellEditor)仅在用户双击单元格时显示。因此,看起来单元格仅在 MouseEvents 上进入“编辑”模式,而不是在它具有焦点时。

如何让单元格在获得焦点后立即进入编辑模式?

【问题讨论】:

    标签: java swing jtable


    【解决方案1】:

    谢谢n00213f。您帖子中的主题和示例很有帮助。通过重载 JTable 中的 changeSelection 方法(如线程中所提示的那样),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();
                }
      };
    

    【讨论】:

    • 为什么不使用this 关键字?
    【解决方案2】:

    您可以通过编程方式实现这一点,您只需监听单元格上的焦点事件,允许焦点和编辑,开始编辑。

    更多关于 threadexample

    【讨论】:

      【解决方案3】:

      这是我为我正在从事的项目编写的代码 sn-p。该代码已针对第一列和最后一列中包含不可编辑单元格的表格进行了测试和验证。该类将制表符限制为仅表格的可编辑单元格。它还支持shift-tabbing到tab。

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-12-05
        • 2011-09-27
        • 1970-01-01
        • 1970-01-01
        • 2014-01-25
        相关资源
        最近更新 更多