Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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 join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants