Java Reference
In-Depth Information
L ISTING 16.8
ComboBoxDemo.java
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" ,
countries
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" ),
image views
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 ];
description
29
30
// Declare and create a description pane
31
private DescriptionPane descriptionPane = new DescriptionPane();
combo box
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);
 
 
Search WWH ::




Custom Search