JavaFX的。如何使用FXML使TableViewCell可编辑?

时间:2016-11-18 15:50:12

标签: javafx tableview

我想只使用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>

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

1 个答案:

答案 0 :(得分:0)

感谢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()
    }
}