TableView 单元格请求焦点

TableView cell request Focus

我是 JavaFX 新手,遇到以下问题:

我在 BorderPane 中有一个表视图。我希望它在加载时专注于最后一个 row/1st 列。我尝试了以下方法:

结果是我想要的单元格确实是蓝色的(就像它被选中一样)但第一个单元格有蓝色边框。因此,当我尝试使用箭头键时,所选单元格会移动到第一行。

顺便说一句,我正在使用 TornadoFX。

有什么想法吗?

提前致谢!

class CashflowTab : View() {
    override val root: HBox by fxml()
    private val mController : CashflowController by inject()
    private val mainView : MainView by inject()

    // Get the buttons
    private val buttonCashflow : Button by fxid("btnCashflow")

    init {
        // Setup the buttons
        buttonCashflow.action {
            setupCashflowTable()
        }
    }

    /** Displays the TableView for the Cashflow */
    private fun setupCashflowTable() {
        var initialFocus = true
        // List of entries for the category ComboBox
        val categoryList = mController.getCashFlowCategoryList()


        // Create the table
        val cashTable = tableview<CashEntry>(mController.getCashEntryList()) {
            isEditable = true
            column(Constants.COL_COUNT, CashEntry::countProperty)
            column(Constants.COL_DATE, CashEntry::dateProperty).makeEditable(LocaleDateConverter())
            column(Constants.COL_INCOME, CashEntry::incomeProperty).makeEditable(CurrencyConverter())
            column(Constants.COL_EXPENSES, CashEntry::expensesProperty).makeEditable(CurrencyConverter())
            column(Constants.COL_PROFIT, CashEntry::profitProperty).converter(CurrencyConverter())
            column(Constants.COL_TOTAL_PROFIT, CashEntry::totalProfitProperty).converter(CurrencyConverter())
            column(Constants.COL_COMMENTS, CashEntry::commentsProperty).makeEditable()
            column(Constants.COL_CATEGORY, CashEntry::categoryProperty).useComboBox(categoryList)

            // Scroll to and focus on the last cell on startup
            if (initialFocus) {
                val lastRow = mController.getCashEntryList().size - 1
                requestFocus()
                scrollTo(lastRow)
                focusModel.focus(lastRow)
                selectionModel.select(lastRow)
                initialFocus = false
            }

            onEditCommit {entry ->
                // Update the list
                mController.updateCashEntryList(entry)

                // Move to the next cell
                requestFocus()
                focusModel.focusRightCell()
                @Suppress("UNCHECKED_CAST")
                selectionModel.select(focusModel.focusedCell.row, focusModel.focusedCell.tableColumn as TableColumn<CashEntry, *>)
            }
            enableCellEditing()

            // Enable edit on key typed
            addEventHandler(KeyEvent.KEY_PRESSED) {keyEvent ->
                if (keyEvent.code.isDigitKey || keyEvent.code.isLetterKey) {
                    if (editingCell == null) {
                        val currentSelectedCell = selectedCell
                        if (currentSelectedCell != null && currentSelectedCell.tableColumn.isEditable) {
                            edit(currentSelectedCell.row, currentSelectedCell.tableColumn)
                        }
                    }
                }
            }
        }

        // Add the table to the view
        mainView.root.center = cashTable
        cashTable.tableMenuButtonVisibleProperty()

        // Ensure no other node can get focus
        cashTable.focusedProperty().onChange {
            val focusOwner = currentStage?.scene?.focusOwnerProperty()?.value

            // Check if the focus owner is the table or a cell
            if (focusOwner !is TableView<*> && focusOwner !is TextField) {
                cashTable.requestFocus()
            }
        }
    }
}

你应该使用

Platform.runLater(() -> {
    requestFocus();
    scrollTo(lastRow);
    ...
});

更新 GUI。