Skip to content

Instantly share code, notes, and snippets.

@ahn94
Created July 12, 2015 02:38
Show Gist options
  • Save ahn94/427efd72271e3101025b to your computer and use it in GitHub Desktop.
Save ahn94/427efd72271e3101025b to your computer and use it in GitHub Desktop.
ComboBox is useful
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;
ComboBox<String> comboBox;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
window = primaryStage;
window.setTitle("Austin");
button = new Button("Click Me");
comboBox = new ComboBox<>();
comboBox.getItems().addAll(
"Good",
"Better",
"Best",
"Crappy",
"Shitty"
);
comboBox.setPromptText("How do you rate it?");
comboBox.setOnAction(e -> System.out.println("User selected: " + comboBox.getValue()));
button.setOnAction(e -> printMovie());
comboBox.setEditable(true);
//Layout
VBox layout = new VBox(10);
layout.setPadding(new Insets(20, 20, 20, 20));
layout.getChildren().addAll(comboBox, button);
Scene scene = new Scene(layout, 300, 250);
window.setScene(scene);
window.show();
}
private void printMovie(){
System.out.println(comboBox.getValue());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment