javafx - TableView单元格请求焦点

标签 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()
                }
            }
        }
    }
    

    最佳答案

    你应该用

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

    更新GUI。

    关于javafx - TableView单元格请求焦点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56327041/

    相关文章:

    android - RecyclerView - 字母索引未出现

    kotlin - 无需在 Dagger 中使用组件即可注入(inject) Kotlin 成员字段

    javafx - Tornadofx Javafx - 如何重新加载 View /组件

    javafx - TornadoFX:在ValidationContext中比较2个表单值

    java - 在命令提示符下运行 jar 文件时显示错误 "JavaFX runtime components are missing, and are required to run this application"

    java - 在线程中使用 Javafx TimeLine

    java - 运行时依赖第三方?

    java - 每当鼠标在场景上移动时如何使节点移动(在 JavaFX 中)?

    kotlin - 如果带注释的成员没有被特定 block 包围,则发出 IDE 警告

    kotlin - 从(现有的)Kotlin程序启动TornadoFX App