Question: JAVA FXML : How to code the coordinate of the Building (see Building.txt below) in the Controller.java using the Java FXML? Please find attached my
JAVA FXML: How to code the coordinate of the Building (see Building.txt below) in the Controller.java using the Java FXML? Please find attached my code for your perusal.
1.) sample.fxml
xml version="1.0" encoding="UTF-8"?>
import javafx.scene.image.*?> import javafx.scene.control.*?> import java.lang.*?> import javafx.scene.layout.*?> import javafx.geometry.Insets?> import javafx.scene.layout.GridPane?> import javafx.scene.control.Button?> import javafx.scene.control.Label?> <GridPane alignment="TOP_CENTER" hgap="10" vgap="10" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller"> <columnConstraints> <ColumnConstraints /> columnConstraints> <rowConstraints> <RowConstraints /> rowConstraints> <children> <BorderPane prefHeight="200.0" prefWidth="200.0"> <top> <HBox alignment="CENTER" prefHeight="100.0" prefWidth="200.0" BorderPane.alignment="CENTER"> <children> <Label fx:id="buildingName" text="Label" /> children> HBox> top> <left> <Button fx:id="previousButton" mnemonicParsing="false" text="Previous" BorderPane.alignment="CENTER" /> left> <right> <Button fx:id="nextButton" mnemonicParsing="false" text="Next" BorderPane.alignment="CENTER" /> right> <center> <ImageView fx:id="buildingImage" fitHeight="150.0" fitWidth="200.0" pickOnBounds="true" preserveRatio="true" BorderPane.alignment="CENTER" /> center> <bottom> <Label fx:id="buildingCoordinate" text="Label" BorderPane.alignment="CENTER" /> bottom> BorderPane> children> GridPane>
2.) Controller.java
package sample; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.fxml.FXML; import javafx.fxml.Initializable; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import java.io.FileNotFoundException; import java.net.URL; import java.util.ArrayList; import java.util.ResourceBundle; public class Controller implements Initializable { ArrayList buildings = new ArrayList<>(); int index = 0; String test; Image image; @FXML Label buildingName; @FXML Button nextButton; @FXML Button previousButton; @FXML ImageView buildingImage; @Override public void initialize(URL location, ResourceBundle resources){ try{ buildings = Building.load(); }catch(Exception e){ e.printStackTrace(); } index = 0; buildingName.setText(buildings.get(index).getName()); test = buildings.get(index).getImageName(); image = new Image(test); buildingImage.setImage(image); HOW TO ADD & CODE THE COORDINATE HERE? nextButton.setOnAction(new EventHandler() { @Override public void handle(ActionEvent event) { index ++; buildingName.setText(buildings.get(index).getName()); test = buildings.get(index).getImageName(); image = new Image(test); buildingImage.setImage(image); } }); } }
3.) Building.java
package sample; import java.io.*; import java.io.FileNotFoundException; import java.util.Scanner; import java.util.ArrayList; public class Building { // attributes public static ArrayList buildings = new ArrayList<>(); private String campus; private String name; private Coordinate coordinate; private String imageName; private String buildingCode; // constructors public Building(String campus, String name, float latitude, float longitude, String imageName, String buildingCode) throws InvalidLatitudeException, InvalidLongitudeException { this.campus = campus; this.name = name; try { coordinate = new Coordinate(latitude, longitude); } catch (InvalidLatitudeException e) { throw e; } catch (InvalidLongitudeException e) { throw e; } this.imageName = imageName; this.buildingCode = buildingCode; } // getters and setters public String getCampus() { return campus; } public void setCampus(String campus) { this.campus = campus; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getImageName() { return imageName; } public void setImageName(String imageName) { this.imageName = imageName; } public String getBuildingCode() { return buildingCode; } public void setBuildingCode(String buildingCode) { this.buildingCode = buildingCode; } public Coordinate getCoordinate() { return coordinate; } public void setCoordinate(Coordinate coordinate) { this.coordinate = coordinate; } // toString override @Override public String toString() { return "Building{" + "campus='" + campus + '\'' + ", name='" + name + '\'' + ", latitude=" + coordinate.getLatitude() + ", longitude=" + coordinate.getLongitude() + ", imageName='" + imageName + '\'' + ", buildingCode='" + buildingCode + '\'' + '}'; } public static Building getBuilding(int index ){ return buildings.get(index); } public static ArrayList load() throws Exception { // declare variables String campus, name, imageName, buildingCode; float latitude, longitude; // file contains building data File fileInput = new File("src/sample/Building.txt"); // create scanner for reading data Scanner sc = new Scanner(fileInput); // reading file line by line while (sc.hasNextLine()) { campus = sc.next(); name = sc.next(); latitude = sc.nextFloat(); longitude = sc.nextFloat(); imageName = sc.next(); buildingCode = sc.next(); // create a building object Building b; try { b = new Building(campus, name, latitude, longitude, imageName, buildingCode); } catch (InvalidLatitudeException|InvalidLongitudeException e){ throw e; } // add building object to array list buildings.add(b); } // close the scanner sc.close(); return buildings; } }
4.) Building.txt
Jamestown Davis_Hall 35.998172 -79.918971 dh_ext_side.png DH Jamestown Applied_Technologies 35.999081 -79.917802 at_ext_back.png AT Jamestown James_Williams_Hall 35.99766 -79.917963 jwh_ext_court_door.png JWH
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
