Homework: for loop output to jTextPane based on jTextBox variables
FIRST: I do not want a hand out, or the answer. I just need a few comments so I can find out where I am going wrong.
Second, my code is sloppy. I did not rename my GUI fields so a lot of them are default. I have been working on this for roughly 15 hours.
What I had is a non-GUI mortgage calculator from Java 1 which implemented a loop and displayed monthly payments in various forms such as principal payment, interest payment, etc.
For Java 2 I need to put this in a GUI.
What I have now is a GUI which has 3 radio buttons. Clicking these buttons populate text fields with "default" loan variables. When I press calculate the result is displayed in another text box. I also have a TextPane- this pane needs to show the scroll of my output, the monthly payments broken down. When I run my program it calculates correctly except only shows the last calculated payment in the text pane. It is also number incorrectly, being short by the initial month.
I understand my code is ugly- I do not need a rewrite, but I would and do appreciate any advice on what I am doing incorrectly or sloppily.
I believe the problem lies in my for loop near the bottom.
Code:
package mortgageweek2;
//MortgageWeek2GUI.java
//Created on Dec 11, 2011, 7:06:53 PM
//@author Chris
import javax.swing.*;
import java.awt.*;
import java.text.DecimalFormat; //Added import to output currency correctly
public class MortgageWeek2GUI extends javax.swing.JFrame {
/** Creates new form MortgageWeek2GUI */
int loanAmount; //Declare all variables.
float result;
double rateR;
double termT;
public MortgageWeek2GUI() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
buttonGroup1 = new javax.swing.ButtonGroup();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton();
jTextField1 = new javax.swing.JTextField();
jTextField2 = new javax.swing.JTextField();
jTextField3 = new javax.swing.JTextField();
jTextField4 = new javax.swing.JTextField();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
jLabel6 = new javax.swing.JLabel();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jRadioButton1 = new javax.swing.JRadioButton();
jRadioButton2 = new javax.swing.JRadioButton();
jRadioButton3 = new javax.swing.JRadioButton();
jLabel7 = new javax.swing.JLabel();
jSeparator1 = new javax.swing.JSeparator();
jSeparator2 = new javax.swing.JSeparator();
jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("Java II Mortgage Calculator with GUI");
jLabel2.setText("Christopher Dill 12/11/2011");
jButton1.setText("Exit");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jLabel3.setText("Principle Loan Amount:");
jLabel4.setText("Interest Rate:");
jLabel5.setText("Loan Term in Years:");
jLabel6.setText("Monthly Payment: ");
jButton2.setText("Calculate");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
jButton3.setText("Clear");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
buttonGroup1.add(jRadioButton1);
jRadioButton1.setText("7 years at 5.35%");
jRadioButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jRadioButton1ActionPerformed(evt);
}
});
buttonGroup1.add(jRadioButton2);
jRadioButton2.setText("15 years at 5.5%");
jRadioButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jRadioButton2ActionPerformed(evt);
}
});
buttonGroup1.add(jRadioButton3);
jRadioButton3.setText("30 years at 5.75%");
jRadioButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jRadioButton3ActionPerformed(evt);
}
});
jLabel7.setText("Use the radio buttons to calculate some predetermined loan values");
jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jScrollPane1.setViewportView(jTextArea1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jLabel1)
.addComponent(jLabel2)
.addGroup(layout.createSequentialGroup()
.addComponent(jButton2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton1)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 125, Short.MAX_VALUE))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jSeparator1)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jRadioButton1)
.addComponent(jRadioButton3)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(jRadioButton2)
.addGap(125, 125, 125)))
.addComponent(jLabel7)
.addComponent(jSeparator2, javax.swing.GroupLayout.DEFAULT_SIZE, 326, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel6)
.addComponent(jLabel5)
.addComponent(jLabel4)
.addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 122, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 194, Short.MAX_VALUE)
.addComponent(jTextField2, javax.swing.GroupLayout.DEFAULT_SIZE, 194, Short.MAX_VALUE)
.addComponent(jTextField3, javax.swing.GroupLayout.DEFAULT_SIZE, 194, Short.MAX_VALUE)
.addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE))))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 259, Short.MAX_VALUE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel2)
.addGap(18, 18, 18)
.addComponent(jLabel7)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 12, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jRadioButton1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jRadioButton2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jRadioButton3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(34, 34, 34)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel3))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel4))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel5)
.addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel6))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 55, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButton1, javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jButton3, javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jButton2, javax.swing.GroupLayout.Alignment.TRAILING)))
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 387, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap())
);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// This is the code for the EXIT button.
System.exit(0);
}
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
// This is the code for the CLEAR button.
jTextField1.setText("");
jTextField2.setText("");
jTextField3.setText("");
jTextField4.setText("");
jTextArea1.setText("");
buttonGroup1.clearSelection(); // Make the clear button unselect all radio buttons.
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// Code here will make the calculation run and conver types.
//Link variables to text fields in the GUI.
loanAmount = Integer.parseInt(jTextField1.getText());
rateR = Double.parseDouble(jTextField2.getText());
termT = Double.parseDouble(jTextField3.getText());
DecimalFormat Currency = new DecimalFormat("$#0.00"); // Provides 2 decimal place format for output.
// Perform calculations.
result = (float) ((loanAmount * (rateR /100/12)) / (1 - Math.pow((1 + (rateR/100/12)), -(termT*12)))); // Perform entire calculation.
jTextField4.setText(String.valueOf(Currency.format(result))); // Change value to string and output. Also apply formatting for $ and 2 decimal places.
double termTotal = termT * 12;
double monthlyInterest = loanAmount * (rateR / (12 * 100));
double monthlyPrincipal = result - monthlyInterest;
double balanceLeft = loanAmount - monthlyPrincipal;
double rateCalc = rateR/100/12;
int lineCount = 0;
for (int Number=1; Number < termTotal; Number++) {
// Begin array.
balanceLeft += (balanceLeft * rateCalc); // Changes to the balance throughout the loop processing.
balanceLeft -= result;
monthlyInterest = balanceLeft * rateCalc;
monthlyPrincipal = result - monthlyInterest;
lineCount++;
if (lineCount > 1000) { // This will display 1000 lines at a time.
try {Thread.sleep (0); // Pauses for 0 millaseconds
lineCount = 0;}
catch(InterruptedException e){} // This does nothing except provide Java with a needed argument.
}
jTextArea1.setText(Number + " " +Currency.format(balanceLeft) + // Main output
" " +Currency.format(monthlyInterest) +
" " +Currency.format(monthlyPrincipal));
//jTextArea1.setText(String.valueOf(Currency.format(result))); // Used to text my TextArea1
}}
private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) {
if (jRadioButton1.isSelected()) { // Apply field choices based on radio button selection.
jTextField1.setText("200000");
jTextField2.setText("5.35");
jTextField3.setText("7");}
}
private void jRadioButton2ActionPerformed(java.awt.event.ActionEvent evt) {
if (jRadioButton2.isSelected()) { // Change field variables.
jTextField1.setText("200000");
jTextField2.setText("5.5");
jTextField3.setText("15"); }
}
private void jRadioButton3ActionPerformed(java.awt.event.ActionEvent evt) {
if (jRadioButton3.isSelected()) { // Change to last field variables.
jTextField1.setText("200000");
jTextField2.setText("5.75");
jTextField3.setText("30"); }
}
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(MortgageWeek2GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(MortgageWeek2GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(MortgageWeek2GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(MortgageWeek2GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
// End of variable Declaration and calculation lines.
public void run() {
new MortgageWeek2GUI().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.ButtonGroup buttonGroup1;
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
private javax.swing.JRadioButton jRadioButton1;
private javax.swing.JRadioButton jRadioButton2;
private javax.swing.JRadioButton jRadioButton3;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JSeparator jSeparator1;
private javax.swing.JSeparator jSeparator2;
private javax.swing.JTextArea jTextArea1;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;
private javax.swing.JTextField jTextField3;
private javax.swing.JTextField jTextField4;
// End of variables declaration
}
Re: Homework: for loop output to jTextPane based on jTextBox variables
Welcome to forum. Do not post the same question multiple times.
Re: Homework: for loop output to jTextPane based on jTextBox variables
Hey Eranga, you killed my post! ;)
Abbreviated version:
setText() performs as advertised and sets the text in the text area - effectively replacing what was there. SO you end up with a single line. Check out the JTextArea API docs for something more appropriate.
Do you intend the for loop to run from Number=1? Count how many times the loop will execute. If anything, it'll be the *last* month that is missed.
Number should be number, and readable method names would improve the code.
Re: Homework: for loop output to jTextPane based on jTextBox variables
I could have sworn I had tried the append method. Anyhow, I changed it to append, changed the formatting around, and made it a lower case n- and the code runs correctly, though it still displays one less than correct line numbers and the last result is a negative. Thanks for the help!
And sorry for the double post it was unintentional.
Re: Homework: for loop output to jTextPane based on jTextBox variables
Quote:
it still displays one less than correct line numbers and the last result is a negative
What is the for loop now? And what does the text area display when it's finished?
Re: Homework: for loop output to jTextPane based on jTextBox variables
Your question is about a JTextPane, which doesn't have an append() method.
Also, there is no such thing as a JTextBox.
Be specific and use proper class names when asking a question.
Also, next time post an Short, Self Contained, Correct Example with your question. We don't have time to read through 300+ lines of code to guess what you are doing.