Created
June 9, 2017 23:56
-
-
Save alvareztech/674755b1cf5513a706148362cb5132b8 to your computer and use it in GitHub Desktop.
JavaFX: ChoiceBox demo.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javafx.application.Application; | |
import javafx.beans.value.ChangeListener; | |
import javafx.beans.value.ObservableValue; | |
import javafx.collections.FXCollections; | |
import javafx.collections.ObservableList; | |
import javafx.event.ActionEvent; | |
import javafx.event.EventHandler; | |
import javafx.geometry.Insets; | |
import javafx.scene.Scene; | |
import javafx.scene.control.Button; | |
import javafx.scene.control.ChoiceBox; | |
import javafx.scene.layout.VBox; | |
import javafx.stage.Stage; | |
public class Main extends Application { | |
public static void main(String[] args) { | |
launch(args); | |
} | |
@Override | |
public void start(Stage primaryStage) throws Exception { | |
ObservableList<String> datos = FXCollections.observableArrayList("Simple", "Doble", "Triple"); | |
ChoiceBox choiceBox = new ChoiceBox(datos); | |
// Solo si quieren que pase algo cuando se selecciona | |
choiceBox.getSelectionModel().selectedItemProperty().addListener(new ChangeListener() { | |
@Override | |
public void changed(ObservableValue observable, Object oldValue, Object newValue) { | |
String seleccion = (String) newValue; | |
System.out.println("Seleccionaste: " + seleccion); | |
} | |
}); | |
Button aceptarButton = new Button("Aceptar"); | |
aceptarButton.setOnAction(new EventHandler<ActionEvent>() { | |
@Override | |
public void handle(ActionEvent event) { | |
// Para obtener lo seleccionado | |
String seleccion = (String) choiceBox.getSelectionModel().getSelectedItem(); | |
System.out.println("Seleccionaste: " + seleccion); | |
} | |
}); | |
VBox vBox = new VBox(); | |
vBox.getChildren().addAll(choiceBox, aceptarButton); | |
vBox.setPadding(new Insets(20)); | |
vBox.setSpacing(20); | |
Scene scene = new Scene(vBox, 200, 120); | |
primaryStage.setScene(scene); | |
primaryStage.show(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment