Question: Javafx Coding Issues Requesting help and understanding how to correct and add the following code to Project0 Thank you 1. studentdata.txt will not view in
Javafx Coding Issues
Requesting help and understanding how to correct and add the following code to Project0
Thank you
1. studentdata.txt will not view in the text fields. The file is in the Source Package folder Error was: java.io.FileNotFoundException: scr\studentdata.txt (The system cannot find the path specified)
2. Record # 0 will not view.
3. The CustomPane mainTitle
How to view text in the MainBorderPane
How to make font size bigger
4. Dont understand how to complete this section.
private void calculateFinalGrade() { }
private void calculateLetterGrade() { }
Instructions: the setFields() method has to calculate the Final Grade and then the Letter Grade from the Final Grade. Must convert String values to double such as Double.parseDouble(fields[3]). The Quizzes are in indexes 3, 4, 5 and the Midterm and Final are indexes 6, 7. Call additional methods to keep the code neat.
Final Grade = (average of 3 quizzes converted to 100pt scale * 30%) + (midterm grade * 35%) + (final * 35%) rounded with Math.round().
Each Quiz Grade need to be multiplied by 10 so they are adjusted to a 100 pt. scale. % values can be converted to decimal by dividing by 100 so 35% is .35 in decimal.
Letter Grade can be determined using a multiway if or a swith blick testing the value of the Final Grade. A switch cant test for double values so typecast them to integer such as the theIntFinal = (int)theDoubleFinalGrade: and use switch(theIntFinalGrade) as the switch statement.
A = 90 or above, B+ = 85-89, B = 80-84, C+ = 75-79, C = 70-74, D+ = 65-69, D = 60-64, F = and all other grade values.
Dont set the value to a TextField to double. Convert the calculated Final Grade to String such as tfFinalGrade.setText(String.valueOf(finalGrade));
5. Bottom Buttons
How to space all buttons equally
How to make background colors show (not working)
|<-First and Last->| are blue
<-Previous and Next-> are green
How to change buttons to Orange when you reach the beginning or end of the file
Where and how to add println statement with messages, Your at the beginning and Youre at the end of the file.
Button goes back to original color when another button not producing an error is pressed
My code
package project0;
import java.io.File;
import java.util.ArrayList;
import java.util.Scanner;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.HPos;
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.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import java.util.*;
import java.io.FileReader;
import java.io.IOException;
import javafx.scene.Node;
import javafx.scene.text.Text;
import javafx.scene.text.Font;
import javafx.scene.paint.Color;
// Project0 - JavaFX Application to view information in a text file.
{
//text fields
private TextField tfCourse = new TextField();
private TextField tfLastName = new TextField();
private TextField tfFirstName = new TextField();
private TextField tfQuiz1 = new TextField();
private TextField tfQuiz2 = new TextField();
private TextField tfQuiz3 = new TextField();
private TextField tfMidterm = new TextField();
private TextField tfFinal = new TextField();
private TextField tfFinalGrade = new TextField();
private TextField tfLetterGrade = new TextField();
private Button butFirst = new Button("|<-First");
private Button butPrevious = new Button("<-Previous");
private Button butNext = new Button("Next->");
private Button butLast = new Button("Last->");
private Label lblRecordNumber = new Label("Record Number");
private int currentRecordIndex;
private TextField input;
private Label output;
private ArrayList fileRecords;
//Instance (Attribute) Variables for Application Object
private String Fields;
private Node mainTitle;
private Node CustomPane;
@Override
public void start(Stage primaryStage)
{
int top, right, bottom, left;
String[] fields;
//Set the working directory to the package folder
String currentDirectory;
File file = new File(".");
currentDirectory = file.getAbsolutePath();
System.out.println("Current working directory : "+currentDirectory);
try (Scanner scanner = new Scanner(new File("scr\\studentdata.txt")))
{
//Create a Scanner for the file
Scanner input = new Scanner(file);
fileRecords = getRecords();
setFields(0);
}
catch (Exception e)
{
System.out.println("Error! Problem opening file. Error was: " + e);
}
//Create border pane
BorderPane mainBorderPane = new BorderPane();
//Place nodes in the pane
mainBorderPane.setTop(new CustomPane("This is Project #3"));
mainBorderPane.setCenter(getFieldsGridPane());
mainBorderPane.setBottom(getHBox());
//Center GridPane
GridPane centerPane = new GridPane();
centerPane.setAlignment(Pos.CENTER);
centerPane.setHgap(5);
centerPane.setVgap(5);
centerPane.setPadding(new Insets(top = 25, right = 10, bottom = 10,
left = 10));
//Place nodes in the centerpane: label and textfield (tf)
centerPane.add(new Label("Course:"),0,0);
centerPane.add(tfCourse,1,0);
centerPane.add(new Label("Last Name:"),0,1);
centerPane.add(tfLastName,1,1);
centerPane.add(new Label("First Name:"),0,2);
centerPane.add(tfFirstName,1,2);
centerPane.add(new Label("Quiz 1:"),0,3);
centerPane.add(tfQuiz1,1,3);
centerPane.add(new Label("Quiz 2:"),0,4);
centerPane.add(tfQuiz2,1,4);
centerPane.add(new Label("Quiz 3:"),0,5);
centerPane.add(tfQuiz3,1,5);
centerPane.add(new Label("Midterm:"),0,6);
centerPane.add(tfMidterm,1,6);
centerPane.add(new Label("Final:"),0,7);
centerPane.add(tfFinal,1,7);
centerPane.add(new Label("Final Grade:"),0,8);
centerPane.add(tfFinalGrade,1,8);
centerPane.add(new Label("Letter Grade:"),0,9);
centerPane.add(tfLetterGrade,1,9);
centerPane.add(butFirst,0,11);
centerPane.add(butPrevious,1,11);
centerPane.add(butNext,2,11);
centerPane.add(butLast,3,11);
centerPane.add(lblRecordNumber,1,12);
//Set properties of UI Nodes
tfCourse.setEditable(false);
tfLastName.setEditable(false);
tfFirstName.setEditable(false);
tfQuiz1.setEditable(false);
tfQuiz2.setEditable(false);
tfQuiz3.setEditable(false);
tfMidterm.setEditable(false);
tfFinal.setEditable(false);
tfFinalGrade.setEditable(false);
tfLetterGrade.setEditable(false);
GridPane.setHalignment(butFirst, HPos.LEFT);
GridPane.setHalignment(butPrevious, HPos.CENTER);
GridPane.setHalignment(butNext, HPos.CENTER);
GridPane.setHalignment(butLast, HPos.RIGHT);
GridPane.setHalignment(lblRecordNumber, HPos.RIGHT);
//Create a Scene for centerPane and Increase W & H
Scene mainScene = new Scene(centerPane, 550, 600);
//Display Stage
primaryStage.setTitle("Project 0");
primaryStage.setScene(mainScene);
primaryStage.show();
}
//HBox
private HBox getHBox(){
HBox hBox = new HBox(10);
hBox.setPadding(new Insets(10, 10, 10, 10));
hBox.setAlignment(Pos.CENTER);
//First Button.
Button butFirst = new Button("|<-First");
butFirst.setId("ClickFirstButton");
butFirst.setStyle("-fx-background-color: green;");
hBox.getChildren().add(butFirst);
//First Button - Action Handler
butFirst.setOnAction(e -> {
defineActionEvent("TheFirstButton");
});
//Previous Button.
Button butPrevious = new Button("<-Previous");
butPrevious.setId("ClickPreviousButton");
butPrevious.setStyle("-fx-background-color: blue");
hBox.getChildren().add(butPrevious);
//Previous Button - Action Handler
butPrevious.setOnAction(e -> {
defineActionEvent("ThePreviousButton;");
});
//Next Button.
Button butNext = new Button("Next->");
butNext.setId("ClickNextButton");
butNext.setStyle("-fx-background-color: blue;");
hBox.getChildren().add(butNext);
//Next Button - Action Handler
butNext.setOnAction(e -> {
defineActionEvent("TheNextButton");
});
//Last Button.
Button butLast = new Button("Last->|");
butLast.setId("ClickLastButton");
butLast.setStyle("-fx-background-color: green;");
hBox.getChildren().add(butLast);
//Last Button - Action Handler
butLast.setOnAction(e -> {
defineActionEvent("TheLastButton");
});
return hBox;
}
//Fields method to populate the record
private GridPane getFieldsGridPane(){
GridPane formPane = new GridPane();
int col = 0, row = 0;
formPane.add(new Label ("Course:"), col = 0, row = 0);
formPane.add(tfCourse, col = 1, row = 0);
formPane.add(new Label ("Last Name:"), col = 0, row = 1);
formPane.add(tfLastName, col = 1, row = 1);
formPane.add(new Label ("First Name:"), col = 0, row = 2);
formPane.add(tfFirstName, col = 1, row = 2);
formPane.add(new Label ("Quiz 1:"), col = 0, row = 3);
formPane.add(tfQuiz1, col = 1, row = 3);
formPane.add(new Label ("Quiz 2:"), col = 0, row = 4);
formPane.add(tfQuiz2, col = 1, row = 4);
formPane.add(new Label ("Quiz 3:"), col = 0, row = 5);
formPane.add(tfQuiz3, col = 1, row = 5);
formPane.add(new Label ("Midterm:"), col = 0, row = 6);
formPane.add(tfMidterm, col = 1, row = 6);
formPane.add(new Label ("Final:"), col = 0, row = 7);
formPane.add(tfFinal, col = 1, row = 7);
formPane.add(new Label ("Final Grade:"), col = 0, row = 8);
formPane.add(tfFinalGrade, col = 1, row = 8);
formPane.add(new Label ("Letter Grade:"), col = 0, row = 9);
formPane.add(tfLetterGrade, col = 1, row = 9);
formPane.add(new Button ("|<-First"), col = 0, row = 11);
formPane.add(butFirst, col = 1, row = 11);
formPane.add(new Button ("<-Previous"), col = 0, row = 11);
formPane.add(butPrevious, col = 2, row = 11);
formPane.add(new Button ("Next->"), col = 0, row = 11);
formPane.add(butNext, col = 3, row = 11);
formPane.add(new Button ("Last->|"), col = 0, row = 11);
formPane.add(butLast, col = 4, row = 11);
formPane.add(new Label ("Record #"), col = 0, row = 12);
formPane.add(lblRecordNumber, col = 1, row = 12);
return formPane;
}
//Custom pane to hold label in the top pane
class CustomPane extends StackPane {
public CustomPane(String mainTitle) {
getChildren().add(new Label(mainTitle));
setPadding(new Insets(10, 10, 10, 10));
setAlignment(Pos.CENTER);
}
}
private void calculateFinalGrade() {
}
private void calulateLetterGrade() {
}
public Project0()
{
tfCourse = new TextField();
tfLastName = new TextField();
tfFirstName = new TextField();
tfQuiz1 = new TextField();
tfQuiz2 = new TextField();
tfQuiz3 = new TextField();
tfMidterm = new TextField();
tfFinal = new TextField();
tfFinalGrade = new TextField();
tfLetterGrade = new TextField();
butFirst = new Button("|<-First");
butPrevious = new Button("<-Previous");
butNext = new Button("Next->");
butLast = new Button("Last->|");
lblRecordNumber = new Label();
currentRecordIndex = 0;
} //Project03 Constructor
private void defineActionEvent(final String forNode)
{
System.out.println("Action Event for: " + forNode);
System.out.println("Current Record Index: " + currentRecordIndex);
switch (forNode)
{
case "FirstButton":
if (currentRecordIndex > 0)
{
currentRecordIndex--;
setFields(currentRecordIndex);
}
else
{
System.out.println("Reached the FIRST Record.");
}
break;
case "PreviousButton":
if (currentRecordIndex > 0)
{
currentRecordIndex--;
setFields(currentRecordIndex);
}
else
{
System.out.println("Reached PREVIOUS Record.");
}
break;
case "NextButton":
if (currentRecordIndex < (fileRecords.size() -1))
{
currentRecordIndex++;
setFields(currentRecordIndex);
}
else
{
System.out.println("Reached NEXT Record.");
}
break;
case "LastButton":
if (currentRecordIndex > -1)
{
currentRecordIndex++;
setFields(currentRecordIndex);
}
else
{
System.out.println("Reached the LAST Record.");
}
break;
default:
System.out.println("Error. Undefined object.");
break;
}
}
private static ArrayList getRecords() throws Exception
{
ArrayList theRecords = new ArrayList<>();
Scanner inputFile;
//Read data from a file
inputFile = new Scanner(new File("studentdata.txt"));
while(inputFile.hasNextLine())
{
theRecords.add(inputFile.nextLine());
}
return theRecords;
}
private void setFields(int theIndex)
{
String[] fields = fileRecords.get(theIndex).split(",");
tfCourse.setText(fields[0]);
tfLastName.setText(fields[1]);
tfFirstName.setText(fields[2]);
tfQuiz1.setText(fields[3]);
tfQuiz2.setText(fields[4]);
tfQuiz3.setText(fields[5]);
tfMidterm.setText(fields[6]);
tfFinal.setText(fields[7]);
tfFinalGrade.setText(fields[8]);
tfLetterGrade.setText(fields[9]);
lblRecordNumber.setText(String.format("Record #%3d", theIndex));
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
