Reputation: 49
I'm learning Java for 3 weeks now and Im wondering how I do get the value of the chosen item (just Strings) of a ChoiceBox without lambdas by using a Listener. I dont use lambdas because I want to understand whats behind it. I got the following code:
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) throws Exception {
//BAUSTEINE:
ChoiceBox<String> choiceBox = new ChoiceBox<String>();
choiceBox.getItems().addAll("item1", "item2", "item3");
choiceBox.setValue("item1");
choiceBox.getSelectionModel().selectedItemProperty().addListener()
//THIS IS THE PLACE FOR THE LISTENER CODE WHICH I NEED ;)
//LAYOUT:
VBox vBox = new VBox();
vBox.setPadding(new Insets(20,20,20,20));
vBox.setSpacing(10);
vBox.getChildren().addAll(choiceBox);
//EIGENSCHAFTEN DER SCENE:
Scene scene = new Scene(vBox, 300, 250);
//EIGENSCHAFTEN DER STAGE:
stage.setScene(scene);
//PROGRAMMSTART:
stage.show();
}
}
Upvotes: 0
Views: 305
Reputation: 38694
All these three are identical:
choiceBox.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<String>()
{
@Override
public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue)
{
System.out.println(observable + oldValue + newValue);
}
});
choiceBox.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
System.out.println(observable + oldValue + newValue);
});
choiceBox.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> System.out.println(observable + oldValue + newValue));
Upvotes: 1