Alex Clark
Alex Clark

Reputation: 213

How to change what change listener is being called javafx

I am new to JavaFX, I have two tables on my view, tableA and tableB. I have made Change Listeners for both tables. They look like the following:

this.tableA.getSelectionModel().selectedItemProperty().addListener
    (New ChangeListener<Person> () {
         @Override
         public void changed( ObservableValue< ? extends Person> observable,Perosn oldValue, Person newValue ) {
             updateButtons("View-A");
         }
});

this.tableB.getSelectionModel().selectedItemProperty().addListener
    (New ChangeListener<Car> () {
         @Override
         public void changed( ObservableValue< ? extends Car> observable,Car oldValue, Car newValue ) {
             updateButtons("View-B");
         }
});

So basically what is happening is that the user selects a Person from TableA and it displays all the Car objects that selected person has in TableB. Then they can select each car and change certain properties by the different buttons. BUT after I select a car, if i decide that i want to select a different person. When i select a person from TableA, the program still calls the TableB change listener?

Upvotes: 0

Views: 640

Answers (1)

James_D
James_D

Reputation: 209330

I would expect both listeners to be invoked in the scenario you describe. When the selection in Table A changes, you replace the items that are shown in table B. If an item was previously selected in Table B, the item that is selected in that table will necessarily change when that happens. (Whatever is now selected, it's not what was previously selected as that item is no longer in the table.) So I'd imagine this to be the behavior you want, otherwise the UI would be in an inconsistent state at this point.

When you change the items shown in Table B, you probably want to clear the selection too:

this.tableA.getSelectionModel().selectedItemProperty().addListener
    (new ChangeListener<Person> () {
         @Override
         public void changed( ObservableValue< ? extends Person> observable,Person oldValue, Person newValue ) {
             // update items shown in table B
             tableB.getSelectionModel().clearSelection();
         }
});

and then make sure that you listener for table B's selection model handles the case where newValue is null appropriately.

Upvotes: 1

Related Questions