JavaFX Tutorial - Java ChoiceBox .setSelectionModel (SingleSelectionModel < T > value)








Syntax

ChoiceBox.setSelectionModel(SingleSelectionModel < T > value) has the following syntax.

public final void setSelectionModel(SingleSelectionModel < T > value)

Example

In the following code shows how to use ChoiceBox.setSelectionModel(SingleSelectionModel < T > value) method.

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.SingleSelectionModel;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
/*from ww w .jav  a2  s  .c  o m*/
public class Main extends Application {
  ObservableList<String> cursors = FXCollections.observableArrayList("a","b","c"); 
    @Override
    public void start(Stage stage) {
      ChoiceBox<String> choiceBox = new ChoiceBox<String>();
      choiceBox.setItems(cursors);
      
      SingleSelectionModel ssm = choiceBox.getSelectionModel();
      choiceBox.setSelectionModel(ssm);
      
      
      VBox box = new VBox();
      box.getChildren().add(choiceBox);
      final Scene scene = new Scene(box,300, 250);
      scene.setFill(null);
      stage.setScene(scene);
      stage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}