Hi everyone, I hope you can help me with this. I have everything in my code working properly except for the fact that the payment amounts are being displayed in the command window and I need to display them in the text area, as well as adding scroll bars to the text area. I have been scouring google and I just can't seem to find a way to move the results to the text area, so any help you could give would be much appreciated!
I realize that I am telling the results to go to the command window by using the System.out.printf but I just can't figure out how or what to change that to??
To Clarify, I need help with two things:
1. Move results from command window to textArea
2. Add scroll bars to textArea
Thanks, you all are awesome!
Brandon
Here is my code:
//import necessary libraries
import java.awt.*;
import java.awt.event.*;
import java.text.DecimalFormat;
import javax.swing.*;
import javax.swing.text.*;
public class PaymentCalculatorWeek3 extends JPanel {
//Declare all variables to be used
private JButton button1;
private JButton button2;
private JButton button3;
private JLabel label1;
private JLabel label2;
private JLabel label3;
private JTextField textField1;
private JTextField textField2;
private JTextArea textArea1;
private JComboBox termList;
double[] interest = {5.35, 5.5, 5.75};
int[] term = {7, 15, 30};
double numMonths = 0;
double monthlyInterest = 0;
double monthlyPayment = 0;
double principlePayment = 0;
double interestPayment = 0;
double balance = 0;
java.text.DecimalFormat df= new java.text.DecimalFormat("$,###.00"); // Formats dollar amount outputs to look like money
public PaymentCalculatorWeek3() {
super(new BorderLayout());
label1 = new JLabel ("Loan Amount (eg. $200,000 = 200000): ", JLabel.LEFT);
textField1 = new JTextField(10);
label2 = new JLabel ("Term Length/Interest Rate: ", JLabel.LEFT);
String[] termStrings = { "7 years at 5.35%", "15 years at 5.5%", "30 years at 5.75%" };
termList = new JComboBox(termStrings);
termList.setSelectedIndex(0);
//Create the 1st button that also has a listener event to perform an action when clicked
button1 = new JButton("Calculate");
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
button1ActionPerformed(evt);
}
});
//Create the 2nd button that also has a listener event to perform an action when clicked
button2 = new JButton("Clear/Reset");
button2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
button2ActionPerformed(evt);
}
});
//Create the 3rd button that also has a listener event to perform an action when clicked
button3 = new JButton("Quit");
button3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
button3ActionPerformed(evt);
}
});
//Add the final fields that will display the monthly payment after it is calculated
label3 = new JLabel("Your Monthly Payment is: ");
textField2 = new JTextField();
textArea1 = new JTextArea(5,20);
//Panel Layouts in the next section inspired by the FormattedTextFieldDemo.java found on this page:
//http://docs.oracle.com/javase/tutorial/uiswing/components/formattedtextfield.html#format
//put the labels into a specific panel layout
JPanel labelPane = new JPanel(new GridLayout(0,1));
labelPane.add(label1);
labelPane.add(label2);
labelPane.add(label3);
//put the textFields into a specific panel layout
JPanel fieldPane = new JPanel(new GridLayout(0,1));
fieldPane.add(textField1);
fieldPane.add(termList);
fieldPane.add(textField2);
//put the buttons into a specific panel layout
JPanel buttonPane = new JPanel(new GridLayout(0,1));
buttonPane.add(button1);
buttonPane.add(button2);
buttonPane.add(button3);
//Create a boarder around the items and specify where to show each of the panels previously created.
setBorder(BorderFactory.createEmptyBorder (20,20,20,20));
add(labelPane, BorderLayout.WEST);
add(fieldPane, BorderLayout.CENTER);
add(buttonPane, BorderLayout.LINE_END);
add(textArea1, BorderLayout.SOUTH);
}
//Setup what the quit button is going to do when clicked
private void button3ActionPerformed(ActionEvent evt) {
System.exit(0);
}
//Setup what the clear button will do when clicked
private void button2ActionPerformed(ActionEvent evt) {
textField1.setText("");
termList.setSelectedIndex(0);
textField2.setText("");
textArea1.setText("");
}
//Setup what the calculate button will do when clicked
private void button1ActionPerformed(ActionEvent evt) {
float principle;
//error handling to properly handle if user input is missing in order for calculation to be performed.
//The following code was inspired by fellow team member Robin Meredith
//as well as the information found here: http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html#features
Object source = evt.getSource();
if(source == button1) {
try {
principle = Float.parseFloat(textField1.getText());
} catch(NumberFormatException e) {
// validates input.
JOptionPane.showMessageDialog(null, "Please enter a valid Loan Amount to continue. \n", "MISSING OR INVALID INFORMATION", JOptionPane.ERROR_MESSAGE);
{
Toolkit.getDefaultToolkit().beep();
return;
}
}
}
principle = Float.parseFloat(textField1.getText());
monthlyInterest = (interest[termList.getSelectedIndex()] / (12 * 100)); // Term 0 is the 1st of 3 in the array
numMonths = (term[termList.getSelectedIndex()] * 12);
//Calculate the amortized monthly payment for each loan
monthlyPayment = principle * ( monthlyInterest / (1 - Math.pow(1 + monthlyInterest, -numMonths)));
textField2.setText(String.valueOf(df.format(monthlyPayment))); //Display the monthly payment with a $ in front.
balance = principle;
interestPayment = balance * (interest[termList.getSelectedIndex()] / (12 * 100));
principlePayment = monthlyPayment - interestPayment;
for (int i=0; i < numMonths; ++i) {
System.out.printf("Payment " + (i+1) + ": Interest Paid - $%.2f", + interestPayment );
System.out.printf(" Remaining Balance: $%.2f\n", + (balance - principlePayment));
// These 3 lines make the necessary subtractions so that the loop does not repeat on the same numbers over and over
balance = balance - principlePayment;
interestPayment = balance * (interest[termList.getSelectedIndex()] / (12 * 100));
principlePayment = monthlyPayment - interestPayment;
// textArea1.setText(String.valueOf(df.format(monthlyPayment)));
}
}
//Create the GUI and prepare to show on users screen
private static void createAndShowGUI() {
JFrame frame = new JFrame("PaymentCalculatorWeek3");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new PaymentCalculatorWeek3());
frame.pack();
frame.setVisible(true);
}
//Show the GUI to user
public static void main(String[] args){
createAndShowGUI();
}
}