Skip to content
This repository has been archived by the owner on Sep 28, 2024. It is now read-only.

Unbind TextField text property from non-String property #1346

Open
codezan opened this issue Nov 4, 2021 · 2 comments
Open

Unbind TextField text property from non-String property #1346

codezan opened this issue Nov 4, 2021 · 2 comments

Comments

@codezan
Copy link

codezan commented Nov 4, 2021

Using this guide: https://docs.tornadofx.io/0_subsection/11_editing_models_and_validation

I created a custom View that displays a "FolderModel" class, which holds a nameProperty and a pathProperty.
In the example

private fun editPerson(person: Person?) {
        if (person != null) {
            prevSelection?.apply {
                nameProperty.unbindBidirectional(nameField.textProperty())
                titleProperty.unbindBidirectional(titleField.textProperty())
            }
            nameField.bind(person.nameProperty)
            titleField.bind(person.titleProperty)
            prevSelection = person
        }
    }

binds both String properties to the appropriate fields and unbinds the previous selection as needed.

When trying to recreate this, I run into the problem of unbinding the previous model from the textfield bound to it:

    if(currentFolder != null) {
      currentFolder?.apply {
        nameProperty.unbindBidirectional(nameField.textProperty())
        pathProperty.unbindBidirectional(pathField.textProperty()) // <-- this won't compile because pathProperty is an ObjectProperty<Path>!
      }
    }

    nameField.bind(folder.nameProperty)
    pathField.bind(folder.pathProperty, converter = object: StringConverter<Path>() {
      override fun toString(path: Path?): String = path?.toString() ?: ""
      override fun fromString(string: String?): Path? = string?.let { try { Path.of(string) } catch (e: Exception) { null } }
    })
    currentFolder = folder

What's the proper way to unbind an ObjectProperty from a TextField after it's bound using a StringConverter?

@yamert89
Copy link
Contributor

yamert89 commented Nov 5, 2021

Try use javafx.beans.binding.Bindings.unbindBidirectional(Object property1, Object property2)

@SKeeneCode
Copy link

SKeeneCode commented Nov 5, 2021

The proper way to approach this is to create a FolderViewModal that extends ItemViewModal and inject that into your FolderView and bind it there. Then you can set the item of the FolderViewModal as needed.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants