TableView单元格请求焦点

时间:2019-05-27 13:27:22

标签: javafx kotlin tornadofx

我是JavaFX的新手,并且存在以下问题:

我在BorderPane中有一个tableview。我希望它在加载时集中在最后一行/ 1st列上。我尝试了以下方法:

  • requestfocus()
  • scrollTo()
  • focusModel.focus()
  • selectionModel.select()

发生的是,我想要的单元格确实是蓝色的(好像已被选中),但是第一个单元格具有蓝色边框。因此,当我尝试使用箭头键时,选定的单元格将移动到第一行。

顺便说一句,我正在使用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()
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您应该使用

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

更新GUI。