Skip to content

Instantly share code, notes, and snippets.

@ahn94
Created July 12, 2015 02:16
Show Gist options
  • Save ahn94/c6fc331223d6de7c8e95 to your computer and use it in GitHub Desktop.
Save ahn94/c6fc331223d6de7c8e95 to your computer and use it in GitHub Desktop.
Listens for change in selection of choiebox selection
import javafx.application.Application;
import javafx.application.Platform;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.Stage;
public class Main extends Application {
Stage window;
Scene scene;
Button button;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
window = primaryStage;
window.setTitle("Austin");
button = new Button("Click Me");
ChoiceBox<String> choiceBox = new ChoiceBox<>();
choiceBox.getItems().addAll("Apples", "Bananas", "Bacon", "Ham", "Meatballs");
choiceBox.setValue("Apples");
//Listen for selection changes
choiceBox.getSelectionModel().selectedItemProperty().addListener( (v, oldValue, newValue) -> System.out.println(newValue));
//Layout
VBox layout = new VBox(10);
layout.setPadding(new Insets(20, 20, 20, 20));
layout.getChildren().addAll(choiceBox, button);
Scene scene = new Scene(layout, 300, 250);
window.setScene(scene);
window.show();
}
private void getChoice(ChoiceBox<String> choiceBox){
String food = choiceBox.getValue();
System.out.println("Choice is: " + food);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment