Reputation: 99
I am using javafx to create a pane that asks for Loan Amount and Number of Years to calculate how much the monthly and total payment will be based on an annual interest rate increasing by .125 from 5% to 8%.
I want to print this out in the form of a table below the initial pane. However, I am stuck trying to figure how to add multiple lines of text to a text area variable.
Here's my code:
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.control.TextArea;
public class LoanTable extends Application {
// create TextField and button variables
private TextField tfAmount = new TextField();
private TextField tfYears = new TextField();
private Button btCalc = new Button("Show Table");
private TextArea taResult = new TextArea();
@Override
public void start(Stage primaryStage) {
// create FlowPane and set properties
FlowPane pane = new FlowPane();
pane.setHgap(10);
tfAmount.setPrefColumnCount(3);
tfYears.setPrefColumnCount(3);
pane.getChildren().addAll(new Label("Loan Amount"), tfAmount, new Label("Number of Years"), tfYears, btCalc);
// create ScrollPane and set properties
ScrollPane scrollPane = new ScrollPane(taResult);
taResult.setEditable(false);
// create BorderPane and add FlowPane and ScrollPane
BorderPane borderPane = new BorderPane();
borderPane.setCenter(pane);
borderPane.setBottom(scrollPane);
// create and register a handler
btCalc.setOnAction(e -> calcTable());
// create a scene and place it in the stage
Scene scene = new Scene(borderPane);
primaryStage.setTitle("Loan-Table");
primaryStage.setScene(scene);
primaryStage.show();
}
// create a method to calculate monthly payment
public double calcMonthly(double interestRate) {
double loanAmount = Double.parseDouble(tfAmount.getText());
int years = Integer.parseInt(tfYears.getText());
double monthlyInterestRate = interestRate / 1200;
double monthlyPayment = loanAmount * monthlyInterestRate / (1 - 1 / Math.pow(1 + monthlyInterestRate, years * 12));
return monthlyPayment;
}
// create a method to calculate total payment
public double calcTotal(double interestRate, double monthlyPayment) {
int years = Integer.parseInt(tfYears.getText());
double totalPayment = monthlyPayment * years * 12;
return totalPayment;
}
private void calcTable() {
// get values from text fields
double interestRate = 5;
// display table header
taResult.setText(String.format("%13s%20s%18s\n", "Interest Rate", "Monthly Payment", "Total Payment"));
// print rest of table
for (int i = 1; i < 26; interestRate+=.125, i++) {
double x = calcMonthly(interestRate);
taResult.setText(String.format("%5.3f%19.2f%22.2f\n", interestRate, calcMonthly(interestRate), calcTotal(interestRate, x)));
}
}
public static void main(String args[]) {
Application.launch(args);
}
}
The issue comes at the "// print rest of the table" loop. How do I add multiple lines of text to the taResult variable? When I run this program, I just end up with the last line of the table. With inputs 10000 (for Loan Amount) and 5 (for Number of Years) the output is:
8.000 202.76 12165.84
But my desired output is:
Interest Rate Monthly Payment Total Payment
5.000 188.71 11322.74
5.125 189.29 11357.13
5.250 189.86 11391.59
5.375 190.44 11426.11
5.500 191.01 11460.70
5.625 191.59 11495.35
5.750 192.17 11530.06
5.875 192.75 11564.84
6.000 193.33 11599.68
6.125 193.91 11634.59
6.250 194.49 11669.56
6.375 195.08 11704.59
6.500 195.66 11739.69
6.625 196.25 11774.85
6.750 196.83 11810.08
6.875 197.42 11845.37
7.000 198.01 11880.72
7.125 198.60 11916.14
7.250 199.19 11951.62
7.375 199.79 11987.16
7.500 200.38 12022.77
7.625 200.97 12058.44
7.750 201.57 12094.18
7.875 202.17 12129.97
8.000 202.76 12165.84
I hope I made the issue clear.
Upvotes: 1
Views: 6166
Reputation: 99
Ah, nevermind. I figured it out.
I needed taResult.appendText --- .appendText is what I was looking for.
Sorry for wasting anyone's time that was reading this.
Upvotes: 4