JAVAFX。如何使用FXML使TableViewCell可编辑?

阿列克谢·瓦申科夫(Alexey Vashchenkov)

我想只使用fxml来制作TableViewCell。我该怎么做。

现在我有一个模型类DuplicateFileInfo

class DuplicateFileInfo(var id: Long, var path: String, var editableField: String?) {}

我有TableView

<TableView AnchorPane.bottomAnchor="50.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0"
                editable="true"
                layoutX="121.0" layoutY="6.0" fx:id="duplicatesList">
    <columns>
        <TableColumn prefWidth="300.0" text="%file.filename" fx:id="fileNameColumn" editable="false">
            <cellValueFactory>
                <PropertyValueFactory property="path" />
            </cellValueFactory>
        </TableColumn>
        <TableColumn prefWidth="150.0" text="%file.EditableField" fx:id="editableColumn">
            <cellValueFactory>
                <PropertyValueFactory property="editableField" />
            </cellValueFactory>
            <cellFactory>
                <TextFieldTableCell fx:factory="forTableColumn" />
            </cellFactory>
        </TableColumn>
    </columns>
</TableView>

在这种情况下,我具有可编辑的表格视图。但是在完成编辑后,该值未设置为模型。是否可以不加编码地完成这项工作?

阿列克谢·瓦申科夫(Alexey Vashchenkov)

感谢James_D,我可以得到结果。

Kotlin的模型课应该是这样的

class DuplicateFileInfo(id: Long, path: String, shouldBeDeleted: Boolean) {

    private val id: LongProperty
    private val path: StringProperty
    private val shouldBeDeleted: BooleanProperty

    init {
        this.id = SimpleLongProperty(id)
        this.path = SimpleStringProperty(path)
        this.shouldBeDeleted = SimpleBooleanProperty(shouldBeDeleted)
    }

    fun getId(): Long {
        return id.get()
    }

    fun idProperty(): LongProperty {
        return id
    }

    fun setId(id: Long) {
        this.id.set(id)
    }

    fun getPath(): String {
        return path.get()
    }

    fun pathProperty(): StringProperty {
        return path
    }

    fun setPath(path: String) {
        this.path.set(path)
    }

    var isShouldBeDeleted: Boolean
        get() = shouldBeDeleted.get()
        set(shouldBeDeleted) = this.shouldBeDeleted.set(shouldBeDeleted)

    fun shouldBeDeletedProperty(): BooleanProperty {
        return shouldBeDeleted
    }

    override fun toString(): String {
        val sb = StringBuffer("DuplicateFileInfo{")
        sb.append("id=").append(id.get())
        sb.append(", path=").append(path.get())
        sb.append(", shouldBeDeleted=").append(shouldBeDeleted.get())
        sb.append('}')
        return sb.toString()
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章