1  import javafx.application.Application;
  2  import javafx.stage.Stage;
  3  import javafx.collections.FXCollections;
  4  import javafx.collections.ObservableList;
  5  import javafx.scene.Scene;
  6  import javafx.scene.control.ComboBox;
  7  import javafx.scene.control.Label;
  8  import javafx.scene.image.ImageView;
  9  import javafx.scene.layout.BorderPane;
 10  
 11  public class ComboBoxDemo extends Application {
 12    // Declare an array of Strings for flag titles
 13    private String[] flagTitles = {"Canada", "China", "Denmark", 
 14      "France", "Germany", "India", "Norway", "United Kingdom",
 15      "United States of America"};
 16  
 17    // Declare an ImageView array for the national flags of 9 countries
 18    private ImageView[] flagImage = {new ImageView("image/ca.gif"),
 19      new ImageView("image/china.gif"), 
 20      new ImageView("image/denmark.gif"), 
 21      new ImageView("image/fr.gif"), 
 22      new ImageView("image/germany.gif"),
 23      new ImageView("image/india.gif"), 
 24      new ImageView("image/norway.gif"),
 25      new ImageView("image/uk.gif"), new ImageView("image/us.gif")};
 26  
 27    // Declare an array of strings for flag descriptions
 28    private String[] flagDescription = new String[9];
 29  
 30    // Declare and create a description pane
 31    private DescriptionPane descriptionPane = new DescriptionPane();
 32  
 33    // Create a combo box for selecting countries
 34    private ComboBox<String> cbo = new ComboBox<>(); // flagTitles
 35  
 36    @Override // Override the start method in the Application class
 37    public void start(Stage primaryStage) {
 38      // Set text description
 39      flagDescription[0] = "The Canadian national flag ...";
 40      flagDescription[1] = "Description for China ... ";
 41      flagDescription[2] = "Description for Denmark ... ";
 42      flagDescription[3] = "Description for France ... ";
 43      flagDescription[4] = "Description for Germany ... ";
 44      flagDescription[5] = "Description for India ... ";
 45      flagDescription[6] = "Description for Norway ... ";
 46      flagDescription[7] = "Description for UK ... ";
 47      flagDescription[8] = "Description for US ... ";
 48  
 49      // Set the first country (Canada) for display
 50      setDisplay(0);
 51  
 52      // Add combo box and description pane to the border pane
 53      BorderPane pane = new BorderPane();
 54        
 55      BorderPane paneForComboBox = new BorderPane();
 56      paneForComboBox.setLeft(new Label("Select a country: "));
 57      paneForComboBox.setCenter(cbo);
 58      pane.setTop(paneForComboBox);
 59      cbo.setPrefWidth(400);
 60      cbo.setValue("Canada");
 61      
 62      ObservableList<String> items = 
 63        FXCollections.observableArrayList(flagTitles);
 64      cbo.getItems().addAll(items); // Add items to combo box
 65      pane.setCenter(descriptionPane);
 66      
 67      // Display the selected country
 68      cbo.setOnAction(e -> setDisplay(items.indexOf(cbo.getValue())));
 69      
 70      // Create a scene and place it in the stage
 71      Scene scene = new Scene(pane, 450, 170);
 72      primaryStage.setTitle("ComboBoxDemo"); // Set the stage title
 73      primaryStage.setScene(scene); // Place the scene in the stage
 74      primaryStage.show(); // Display the stage
 75    }
 76  
 77    /** Set display information on the description pane */
 78    public void setDisplay(int index) {
 79      descriptionPane.setTitle(flagTitles[index]);
 80      descriptionPane.setImageView(flagImage[index]);
 81      descriptionPane.setDescription(flagDescription[index]);
 82    }
 83  
 84    /**
 85     * The main method is only needed for the IDE with limited
 86     * JavaFX support. Not needed for running from the command line.
 87     */
 88    public static void main(String[] args) {
 89      launch(args);
 90    }
 91  }