gpt4 book ai didi

javafx - TableView单元格请求焦点

转载 作者:行者123 更新时间:2023-12-02 12:58:38 27 4
gpt4 key购买 nike

我是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/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com