Question: Anthony needs help with Java Programming as soon as possible. Anthony wrote: Hi I have the below code for java fx, but I cannot get

Anthony needs help with Java Programming as soon as possible. Anthony wrote: Hi I have the below code for java fx, but I cannot get the csv to populate in last screen opened, the csv is in correct area, and I tested it out separately and it works on simpler code-think something must be incorrect? Hope you can help. import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import javafx.application.Application; import javafx.application.Platform; import javafx.geometry.Insets; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.control.ListView; import javafx.scene.control.TextArea; import javafx.scene.control.TextField; import javafx.scene.layout.BorderPane; import javafx.scene.layout.GridPane; import javafx.scene.layout.HBox; import javafx.scene.layout.VBox; import javafx.stage.Stage; public class FXMail extends Application { Stage window; Scene scene, scene1; ListView listView; //Labels to identify controls. Label lblNames, lblEmail, lblAddress; //Textfield to show email. TextField txtfEmail; //Textarea to show address. TextArea txtAddress; //Button to close the application. Button btnClose; //Listview to show list of contact names. ListView lvNames; //public FXMail() { // TODO Auto-generated constructor stub //}//constructor @Override public void init(){ //Instantiate controls. lblNames = new Label("Names:"); lblEmail = new Label("Email:"); lblAddress = new Label("Address:"); lvNames = new ListView (); txtfEmail = new TextField(); txtAddress = new TextArea(); btnClose = new Button("Close"); btnClose.setOnAction(ae -> Platform.exit()); lvNames.setOnMouseClicked(ae -> { //Get the selected name. String selectedName = lvNames.getSelectionModel().getSelectedItem(); //Call the method that populates the email and address fields. showEmailAndAddr("./contacts.csv", selectedName); }); }//init() private void showEmailAndAddr(String fileName, String selection){ //Read in the contact names from a file. //try to open a file. try{ String line; FileReader fr = new FileReader(fileName); BufferedReader buf = new BufferedReader(fr); while((line = buf.readLine()) != null){ if(line.startsWith(selection)){ //A 2-item array to hold the split line contents. String [] contactDataArray = new String[3]; //Split the string into 3 on ':' char. contactDataArray = line.split(":"); //Add the email to the email textfield txtfEmail.setText(contactDataArray[1]); //Add the address to the address textarea. txtAddress.setText(contactDataArray[2]); }//if }//while() //Done iterating through file lines. Close the file. buf.close(); }//try catch(IOException ioe){ ioe.toString(); }//catch }//showEmailAndAddr private void readContactNames(String contactsFile){ //Read in the contact names from a file. //try to open a file. try{ String line; FileReader fr = new FileReader(contactsFile); BufferedReader buf = new BufferedReader(fr); while((line = buf.readLine()) != null){ //A 3-item array to hold the split line contents. String [] contactDataArray = new String[3]; //Split the string into 2 on ':' char. contactDataArray = line.split(":"); //Add just the name to the listview. lvNames.getItems().add(contactDataArray[0]); }//while() //Done iterating through file lines. Close the file. buf.close(); //Sort the list of names in the listview. lvNames.getItems().sort(null); }//try catch(IOException ioe){ ioe.toString(); }//catch }//readContactNames() @Override public void start(Stage primaryStage) throws Exception { window = primaryStage; window.setTitle("FXMail"); //Layout 1 //GridPane with 8x padding around edge GridPane grid = new GridPane(); grid.setPadding(new Insets(8, 8, 8, 8)); grid.setVgap(10); grid.setHgap(10); grid.setAlignment(Pos.BASELINE_LEFT); //From Label - constrains use (child, column, row) Label fromLabel = new Label("From:"); GridPane.setConstraints(fromLabel, 0, 0); //from Input TextField fromInput = new TextField(); GridPane.setConstraints(fromInput, 1, 0); fromInput.setAlignment(Pos.BASELINE_RIGHT); //Subject Label Label subjectLabel = new Label("Subject:"); GridPane.setConstraints(subjectLabel, 0, 1); //Subject Input TextField subjectInput = new TextField(); GridPane.setConstraints(subjectInput, 1, 1); //To Label Label toLabel = new Label("To:"); GridPane.setConstraints(toLabel, 0, 2); //Subject Input TextField toInput = new TextField(); GridPane.setConstraints(toInput, 1, 2); //sidebutton-clicks to Dialog Button sideButton = new Button("..."); GridPane.setConstraints(sideButton, 2, 2); sideButton.setOnAction(e-> showAboutDialog()); TextArea textArea= new TextArea(); //textArea.setPrefHeight(200); //textArea.setPrefWidth(500); GridPane.setConstraints(textArea, 0, 3,3,3); //Ready Label - constrains use (child, column, row) Label readyLabel = new Label("Ready"); GridPane.setConstraints(readyLabel, 0, 7); //Quitbutton Button quitButton = new Button("Quit"); quitButton.setOnAction(ae -> Platform.exit()); GridPane.setConstraints(quitButton, 3, 6); //Sendbutton Button sendButton = new Button("Send"); GridPane.setConstraints(sendButton, 4, 6); //Add everything to grid grid.getChildren().addAll(fromLabel, fromInput, subjectLabel, subjectInput,toLabel, toInput, sideButton, textArea, readyLabel, quitButton, sendButton); Scene scene = new Scene(grid, 400, 300); window.setScene(scene); window.show(); //Layout 2 Label label1 = new Label("Addresses"); Button button1 = new Button("Go back"); Button button2 = new Button("Address book"); button1.setOnAction(e-> window.setScene(scene)); VBox layout2 = new VBox(20); layout2.getChildren().addAll(label1, button1, button2); scene1 = new Scene(layout2, 200, 200); //Create a second layout. VBox vbNames = new VBox(); vbNames.setSpacing(5); VBox vbAddresses = new VBox(); vbAddresses.setSpacing(5); HBox hbButton = new HBox(); hbButton.getChildren().add(btnClose); hbButton.setAlignment(Pos.BASELINE_RIGHT); GridPane gpMain = new GridPane(); gpMain.setPadding(new Insets(5)); gpMain.setHgap(10); gpMain.setVgap(10); //Add components to the layout. vbNames.getChildren().addAll(lblNames, lvNames); vbAddresses.getChildren().addAll(lblEmail, txtfEmail, lblAddress, txtAddress); gpMain.add(vbNames, 0, 0); gpMain.add(vbAddresses, 1, 0); gpMain.add(hbButton, 1, 1); scene1 = new Scene(gpMain ); //Set the scene. //window.setScene(scene1); //Populate the listview with names. readContactNames("./contacts.csv"); //Show the stage. //window.show(); } //this is for the Dialog box public void showAboutDialog() { Stage dialogStage = new Stage(); dialogStage.setWidth(400); dialogStage.setHeight(200); dialogStage.setTitle("Address Book"); dialogStage.setResizable(false); dialogStage.setMaximized(false); dialogStage.setAlwaysOnTop(true); //Label for a msg Label lblDirectory =new Label (""); //Ok button Button btnOK= new Button ("OK"); //Cancel button Button btnCancel= new Button ("Cancel"); Button btnAddresses= new Button ("Addresses"); //Handle clicks on the buttons btnOK.setOnAction(ae -> dialogStage.close()); btnCancel.setOnAction(ae -> dialogStage.close()); btnAddresses.setOnAction(e-> window.setScene(scene1)); BorderPane bp =new BorderPane(); bp.setCenter(lblDirectory); bp.setBottom(btnOK); bp.setRight(btnCancel); bp.setLeft(btnAddresses); Scene scene3 =new Scene (bp, 400,200); ///set the scene dialogStage.setScene(scene3); //show the stage dialogStage.show(); } public static void main(String[] args) { launch(args); }}//start()

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!