edvin / tornadofx

Lightweight JavaFX Framework for Kotlin

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Dirty TextField

youndon opened this issue · comments

I'm trying create simple CRUD Application by Tornadofx, here's the code:

fun main() =  <Dirty>()

class Dirty:App(DirtyView::class)

class DirtyView:View(){
    private var peopleTable : TableView<People> by singleAssign()
    private val model = PeopleModel(People())
    private val listpeople = FXCollections.observableArrayList(People("firstname","lastname"),People("firstname1","lastname1"))
    override val root = borderpane {
            left {
                tableview(listpeople) {
                    peopleTable = this
                    column("FIRST NAME", People::firstnameProperty)
                    column("LAST NAME", People::lastnameProperty)
                    smartResize()
                    model.rebindOnChange(this) {
                        item = it  ?: People()
                    }
                }
            }
        right{
            form{
                fieldset("Edit People.") {
                    field("First Name*") {
                        textfield(model.bindfirstname) {
                            model.dirty.addListener { a,b,dirty->
                                if (dirty) style { textFill= Color.RED } else style{textFill=Color.BLACK}
                            }
                        }
                    }
                    field("Last Name*") {
                        textfield(model.bindlastname) {  }
                    }
                }
            }
        }
    }
}
 class People(firstname:String?=null, lastname:String?=null){
      val firstnameProperty = SimpleStringProperty(firstname)
     var firstname: String by firstnameProperty
     val lastnameProperty = SimpleStringProperty(lastname)
     var lastname by lastnameProperty
 }
class PeopleModel(people:People):ItemViewModel<People>(people){
    val bindfirstname = bind(People::firstnameProperty)
    val bindlastname = bind(People::lastnameProperty)
}

And it's work so well, but what i want is make the text color change into red color when i make some edit , I used code like :

   model.dirty.addListener { a,b,dirty->
                                if (dirty) style { textFill= Color.RED } else style{textFill=Color.BLACK}
                            }

but the issue in this is if i make edit in lastname Field the firstname Text color also change here, And i wanna make the change color only in the field when some edit is happen

good question, luckily it has a simple answer.

you can check when a particular property is dirty:

textfield(model.bindfirstname) {
    model.bindfirstname.onChange {
         if (model.bindfirstname.isDirty) style { textFill= Color.RED } else style{textFill=Color.BLACK}
    }
}

Ops! That's it?
Thanks a lot for your responding me, i test the code and it's work so well.