JavaFX 11 TableView Cell navigation by TAB key pressed without custom editableCell class

旧时模样 提交于 2019-12-13 03:47:32

问题


The problem:

I want to navigate through a TableView from one cell to the next right neighbor cell in JavaFX by using the TAB key.

Notice: The TableView is set to editable. And CellSelection is enabled too.

 tableReceipt.getSelectionModel().setCellSelectionEnabled(true);

The handling of the KeyPressedEvent seemingly is not my problem, but to request the focus of the single cell on the right of the current cell.

I can focus one cell but when i press the TAB key the focus goes out of the table on other form elements.

The TableView contains some editable TextFieldTableCells and one editable ComboBoxTableCell.

I don't use a custom class for the editable Cells but Code like this:

Callback<TableColumn<Receipt, int>, TableCell<Receipt, int>> tfCallBack = TextFieldTableCell.forTableColumn();
columnAmount.setCellFactory(tfCallBack);

for a TableCell with editable TextField nature.

My question:

How can I implement a solution to solve my problem? A theoretical solution would help too. I allready searched for this topic but only found an example that's using a custom EditableCell class. I think there must be a solution using the callback method like I do.

Solution approach:

tableReceipt.setOnKeyPressed(new EventHandler<KeyEvent>() {
    @Override
    public void handle(KeyEvent t) {
        if (t.getCode() == KeyCode.TAB) {
            tableReceipt.getFocusModel().focusRightCell();
            Robot r = new Robot();
            r.keyPress(KeyCode.ENTER);
         }
     }
});

With this code I can get focus of the right cell next to the current one. And I need the ENTER KeyPress to enable the editable mode of the Cell. But when I press TAB on keyboard the new value is not committed. For example I press '2' the default value is '0' and after pressing TAB the value is again '0'.

Question No.2:

How can I combine the code above with a changeListener/onEditCommitListener, that the new value is stored in the cell after pressing TAB?

Thank you.

来源:https://stackoverflow.com/questions/55283510/javafx-11-tableview-cell-navigation-by-tab-key-pressed-without-custom-editablece

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!