Issue
choiceBox.getSelectionModel().selectedItemProperty().addListener( (v, oldValue, newValue) -> {selectedColor = newValue.toString();});
System.out.println(selectedColor);
I want to pass the newValue to selectedColor but outside the lambda it's not printing anything.
Solution
Unless you provide more details on what you want to accomplish i can only guess what would help you. But the following code would make your example work the way you describe it:
choiceBox.getSelectionModel().selectedItemProperty().addListener((v,oldValue,newValue) -> {
printSelection(newValue.toString());
});
private void printSelection(String selection){
System.out.println(selection);
//Here you can do what ever you need to do with the given value of 'selection'
}
For a better solution you need to provide more Details on what your goal is, what you want to use the newValue
for
Answered By - sxeros
Answer Checked By - Marilyn (JavaFixing Volunteer)