Pages

Wednesday, 4 January 2017

16.27 (Display country flag and flag description)
Listing 16.4, ComboBoxDemo.java, gives a program that
lets the user view a country’s flag image and description
by selecting the country from a combo box.
The description is a string coded in the program.
Rewrite the program to read the text description from a file.
Suppose that the descriptions are stored in the files description0.txt, . . . , and
description8.txt under the text directory for the nine countries Canada,
China, Denmark, France, Germany, India, Norway, United Kingdom,
and United States, in this order.

importjavafx.application.Application;importjavafx.collections.FXCollections;importjavafx.collections.ObservableList;importjavafx.geometry.Insets;importjavafx.scene.Scene;importjavafx.scene.control.*;importjavafx.scene.image.ImageView;importjavafx.scene.layout.BorderPane;importjavafx.scene.text.Font;importjavafx.stage.Stage;importjava.io.File;importjava.io.FileNotFoundException;importjava.util.Scanner;publicclassExercise_27extends Application {// Declare an array of Strings for flag titlesprivate String[] flagTitles ={"Canada","China","Denmark","France","Germany","India","Norway","United Kingdom","United States of America"};// Declare an ImageView array for the national flags of 9 countriesprivate ImageView[] flagImage ={new ImageView("image/ca.gif"),newImageView("image/china.gif"),newImageView("image/denmark.gif"),newImageView("image/fr.gif"),newImageView("image/germany.gif"),newImageView("image/india.gif"),newImageView("image/norway.gif"),newImageView("image/uk.gif"),new ImageView("image/us.gif")};// Declare an array of strings for flag descriptionsprivate String[] flagDescription =new String[9];// Declare and create a description paneprivate DescriptionPane descriptionPane =new DescriptionPane();// Create a combo box for selecting countriesprivate ComboBox<String> cbo =new ComboBox<>();// flagTitles);@Override// Override the start method in the Application classpublicvoidstart(Stage primaryStage){// Set text descriptionfor(int i =0; i < flagDescription.length; i++){
Scanner input;
String s ="";try{
input =new Scanner(new File("src/Text Files/description"+ i +".txt"));while(input.hasNext()){
s += input.nextLine()+"\n";}
flagDescription[i]= s;}catch(FileNotFoundException e){
e.printStackTrace();}}// Set the first country (Canada) for display
setDisplay(0);// Add combo box and description pane to the border pane
BorderPane pane =new BorderPane();
BorderPane paneForComboBox =new BorderPane();
paneForComboBox.setLeft(new Label("Select a country: "));
paneForComboBox.setCenter(cbo);
pane.setTop(paneForComboBox);
cbo.setPrefWidth(400);
cbo.setValue("Canada");
ObservableList<String> items =
FXCollections.observableArrayList(flagTitles);
cbo.getItems().addAll(items);
pane.setCenter(descriptionPane);// Display the selected country
cbo.setOnAction(e -> setDisplay(items.indexOf(cbo.getValue())));// Create a scene and place it in the stage
Scene scene =new Scene(pane,450,170);
primaryStage.setTitle("ComboBoxDemo");// Set the stage title
primaryStage.setScene(scene);// Place the scene in the stage
primaryStage.show();// Display the stage}/** Set display information on the description pane */publicvoidsetDisplay(int index){
descriptionPane.setTitle(flagTitles[index]);
descriptionPane.setImageView(flagImage[index]);
descriptionPane.setDescription(flagDescription[index]);}/** * The main method is only needed for the IDE with limited * JavaFX support. Not needed for running from the command line. */publicstaticvoidmain(String[] args){
launch(args);}privateclassDescriptionPaneextends BorderPane {/** Label for displaying an image and a title */private Label lblImageTitle =new Label();/** Text area for displaying text */private TextArea taDescription =new TextArea();publicDescriptionPane(){// Center the icon and text and place the text under the icon
lblImageTitle.setContentDisplay(ContentDisplay.TOP);
lblImageTitle.setPrefSize(200,100);// Set the font in the lbl and the text field
lblImageTitle.setFont(new Font("SansSerif",16));
taDescription.setFont(new Font("Serif",14));
taDescription.setWrapText(true);
taDescription.setEditable(false);// Create a scroll pane to hold the text area
ScrollPane scrollPane =new ScrollPane(taDescription);// Place lbl and scroll pane in the border pane
setLeft(lblImageTitle);
setCenter(scrollPane);
setPadding(new Insets(5,5,5,5));}/** Set the title */publicvoidsetTitle(String title){
lblImageTitle.setText(title);}/** Set the image view */publicvoidsetImageView(ImageView icon){
lblImageTitle.setGraphic(icon);}/** Set the text description */publicvoidsetDescription(String text){
taDescription.setText(text);}}}