G
G
gubber2016-11-18 19:27:14
Kotlin
gubber, 2016-11-18 19:27:14

JavaFx. Is it possible to make an editable cell without code?

Is it possible to make a correctly editable cell in a table without using code?
I have a model class:

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

And there is a table to display
<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>

Now it turns out that I correctly display the value of the editableField field. Also, the column is editable. But, the results of editing do not fall into the original object.
Is it possible to ensure that the results of editing fall into the original model object without writing code?
Perhaps you need to rewrite the model class, this option is also suitable. I don't want to write CellFactory for each column in the code.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
gubber, 2016-12-01
@gubber

It's all about the model class. It must match the PropertyPattern for JavaFx.
The working class of the model with which I managed to achieve the result is as follows

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

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question