SelectionMode.MULTIPLE与TAB一起导航到下一个单元格

时间:2018-09-19 08:06:58

标签: javafx

我当前正在使用TAB导航到下一个单元格。当我使用selectNext()时,selectRightCell()SelectionMode.SINGLE可以正常工作。

但是,当使用SelectionMode.MULTIPLE时,它会选择多个单元格作为I TAB。

我正在使用TableView。我需要SelectionMode.MULTIPLE才能执行复制和粘贴功能。

有没有办法使其在SelectionMode.MULTIPLE中工作?

    fixedTable.addEventFilter(KeyEvent.KEY_PRESSED, new EventHandler<KeyEvent>() {
        @Override
        public void handle(KeyEvent event) {
            switch (event.getCode()){
                case TAB:
                    if (event.isShiftDown()) {
                        fixedTable.getSelectionModel().selectPrevious();                      
                    } else {
                        fixedTable.getSelectionModel().selectNext();
                    }
                    event.consume();
                    break;

                case ENTER:
                    return;

                case C:
                    if(event.isControlDown()){
                        copySelectionToClipboard(fixedTable) ;
                    }
                    event.consume();
                    break;

                case V:
                    if(event.isControlDown()){
                        pasteFromClipboard(fixedTable);
                    }
                    event.consume();
                    break;

                default:
                    if (fixedTable.getEditingCell() == null) {
                        if (event.getCode().isLetterKey() || event.getCode().isDigitKey()) {
                            TablePosition focusedCellPosition = fixedTable.getFocusModel().getFocusedCell();
                            fixedTable.edit(focusedCellPosition.getRow(), focusedCellPosition.getTableColumn());
                        }
                    }
                    break;
            }
        }
    });

1 个答案:

答案 0 :(得分:0)

您将需要自行处理选择。原因是因为方法selectPrevious()selectNext()试图选择上一个(或下一个)而不删除当前选定的行(当您将选择模式设置为SelectionMode.MULTIPLE时)您不能使用它们,只能通过调用clearSelection()来删除上一个选择,因为这会将所选索引设置为-1,然后方法selectPrevious()selectNext()将选择最后一个或仅第一行。

这是您自己实现选择的方法:

// the rest of your switch statement
...
case TAB:
    // Find the current selected row
    int currentSelection = table.getSelectionModel().getSelectedIndex();
    // remove the previous selection
    table.getSelectionModel().clearSelection();
    if (event.isShiftDown()) {
        currentSelection--;
    } else {
        currentSelection++;
    }

    // find the size of our table
    int size = table.getItems().size() - 1;

    // we was on the first element and we try to go back
    if(currentSelection < 0){
        // either do nothing or select the last entry
        table.getSelectionModel().select(size);
    }else if(currentSelection > size) {
        // we are at the last index, do nothing or go to 0
        table.getSelectionModel().select(0);
    }else {
        // we are between (0,size) 
        table.getSelectionModel().select(currentSelection);
    }
    event.consume();
    break;
...