简体   繁体   English

当按回车键时,将焦点放在下一个单元格编辑器上并在JTable中出售其中的所有文本

[英]focus next cellEditor and sellect all text in it in JTable when press enter key

I want the enter key to act like tab key on JTable. 我希望Enter键像JTable上的Tab键一样工作。 I know that this problem has been asked a few time already, and i used a solution found in this page : Use Enter Key Act Like Tab Key on jTable . 我知道这个问题已经问了好几次了,我使用了在此页面中找到的解决方案: 在jTable上使用Enter键就像Tab键

    KeyStroke tab = KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0);
    KeyStroke enter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0);
    InputMap im = table.getInputMap(JTable.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    im.put(enter, im.get(tab));

and the solution work like a charm. 解决方案就像一个魅力。 but when using tab key, I've overridden the changeSelection of JTable,and override getTableCellEditorComponent of the cell editor to selectAll text in the textField in the next cell : 但是当使用Tab键时,我已经覆盖了JTable的changeSelection,并且覆盖了单元格编辑器的getTableCellEditorComponent,以选择下一个单元格中textField中的所有文本:

in JTable 在JTable中

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

an in CellEditor 在CellEditor中

    @Override
    public Component getTableCellEditorComponent(JTable table,
            Object value, boolean isSelected, int row, int column) {

           if (isSelected ) {
                SwingUtilities.invokeLater(new Runnable() {

                 @Override
                 public void run() {
                     textField.selectAll();
                 }
                });
          }
          return super.getTableCellEditorComponent(
                table, value, isSelected, row, column);
    }

so when i stroke the tab key the next cell is focused and the text in it is sellected, but when i type the enter key the first time the cell loose focus, and the second enter the next cell gain focus and the text in it is selected. 因此,当我敲击Tab键时,下一个单元格将被聚焦,并且其中的文本将被显示,但是当我第一次键入Enter键时,单元格将失去焦点,第二次输入下一个单元格将获得焦点,并且其中的文本是已选择。

so my question is: Is there a way so the first enter loose focus from the cell and gain the focus for the next one (so it will act exactly like the tab key) 所以我的问题是:有没有办法让第一个从单元格输入松散的焦点并获得下一个焦点的焦点(所以它的作用就像Tab键一样)

Don't know if it will make a difference but I've just overridden the changeSelection(...) method to do the cell selection: 不知道它是否会有所作为,但是我只是重写了changeSelection(...)方法来进行单元格选择:

if (editCellAt(row, column))
{
    Component editor = getEditorComponent();
    editor.requestFocusInWindow();
    ((JTextComponent)editor).selectAll();
}

Post a SSCCE that demonstrates the problem if that doesn't help. 如果无济于事 ,请发布一个SSCCE来演示问题。

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

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