'How to return value from lambda expression? (JavaFx- ChoiceBox addListener) [closed]
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 1:[1]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | sxeros |
