Basic newbie question:
I want to sync/bind two tables.
For keeping the example simple, I have used two separate table views. This needs to be done using fragments and scope, which I thought would complicate the question as I am stuck at a basic problem.
Behaviour: On clicking the sync button of table 1 , I want table 1 selected data to override the corresponding table 2 data. and vice-versa
Person Model:
class Person(firstName: String = "", lastName: String = "") {
val firstNameProperty = SimpleStringProperty(firstName)
var firstName by firstNameProperty
val lastNameProperty = SimpleStringProperty(lastName)
var lastName by lastNameProperty
}
class PersonModel : ItemViewModel<Person>() {
val firstName = bind { item?.firstNameProperty }
val lastName = bind { item?.lastNameProperty }
}
Person Controller (dummy data):
class PersonController : Controller(){
val persons = FXCollections.observableArrayList<Person>()
val newPersons = FXCollections.observableArrayList<Person>()
init {
persons += Person("Dead", "Stark")
persons += Person("Tyrion", "Lannister")
persons += Person("Arya", "Stark")
persons += Person("Daenerys", "Targaryen")
newPersons += Person("Ned", "Stark")
newPersons += Person("Tyrion", "Janitor")
newPersons += Person("Arya", "Stark")
newPersons += Person("Taenerys", "Dargaryen")
}
}
Person List View:
class PersonList : View() {
val ctrl: PersonController by inject()
val model : PersonModel by inject()
var personTable : TableView<Person> by singleAssign()
override val root = VBox()
init {
with(root) {
tableview(ctrl.persons) {
personTable = this
column("First Name", Person::firstNameProperty)
column("Last Name", Person::lastNameProperty)
columnResizePolicy = SmartResize.POLICY
}
hbox {
button("Sync") {
setOnAction {
personTable.bindSelected(model)
//model.itemProperty.bind(personTable.selectionModel.selectedItemProperty())
}
}
}
}
}
Another Person List View:
class AnotherPersonList : View() {
val model : PersonModel by inject()
val ctrl: PersonController by inject()
override val root = VBox()
var newPersonTable : TableView<Person> by singleAssign()
init {
with(root) {
tableview(ctrl.newPersons) {
newPersonTable = this
column("First Name", Person::firstNameProperty)
column("Last Name", Person::lastNameProperty)
columnResizePolicy = SmartResize.POLICY
}
hbox {
button("Sync") {
setOnAction {
newPersonTable.bindSelected(model)
}
}
}
}
}
}

bindSelectedon both. Note that thebindSelectedcall should be called once, and not when you click an action. What is does is simply make sure that a table selection will update the model - not when you call bindSelection, but when the selection occurs. Not completely sure of your actual use case, but let me try to rewrite it a bit. - Edvin Syse