Need help with JTextArea and Scrolling
import java.awt.*;
import java.awt.event.*;
import java.text.DecimalFormat;
import javax.swing.*;
publicclass MORT_RETRYextends JFrameimplements ActionListener
{
private JPanel keypad;
private JPanel buttons;
private JTextField lcdLoanAmt;
private JTextField lcdInterestRate;
private JTextField lcdTerm;
private JTextField lcdMonthlyPmt;
private JTextArea displayArea;
private JButton CalculateBtn;
private JButton ClrBtn;
private JButton CloseBtn;
private JButton Amortize;
private JScrollPane scroll;
private DecimalFormat calcPattern =new DecimalFormat("$###,###.00");
private String[] rateTerm ={"","7years @ 5.35%","15years @ 5.5%","30years @ 5.75%"};
private JComboBox rateTermList;
double interest[] ={5.35, 5.5, 5.75};
int term[] ={7, 15, 30};
double balance, interestAmt, monthlyInterest, monthlyPayment, monPmtInt, monPmtPrin;
int termInMonths, month, termLoop, monthLoop;
public MORT_RETRY()
{
Container pane = getContentPane();
lcdLoanAmt =new JTextField();
lcdMonthlyPmt =new JTextField();
displayArea =new JTextArea();
//DEFINE COMBOBOX AND SCROLL
rateTermList =new JComboBox(rateTerm);
scroll =new JScrollPane(displayArea);
scroll.setSize(600,170);
scroll.setLocation(150,270);
//DEFINE BUTTONS
CalculateBtn =new JButton("Calculate");
ClrBtn =new JButton("Clear Fields");
CloseBtn =new JButton("Close");
Amortize =new JButton("Amortize");
//DEFINE PANEL(S)
keypad =new JPanel();
buttons =new JPanel();
//DEFINE KEYPAD PANEL LAYOUT
keypad.setLayout(new GridLayout( 4, 2, 5, 5));
//SET CONTROLS ON KEYPAD PANEL
keypad.add(new JLabel("Loan Amount$ : "));
keypad.add(lcdLoanAmt);
keypad.add(new JLabel("Term of loan and Interest Rate: "));
keypad.add(rateTermList);
keypad.add(new JLabel("Monthly Payment : "));
keypad.add(lcdMonthlyPmt);
lcdMonthlyPmt.setEditable(false);
keypad.add(new JLabel("Amortize Table:"));
keypad.add(displayArea);
displayArea.setEditable(false);
//DEFINE BUTTONS PANEL LAYOUT
buttons.setLayout(new GridLayout( 1, 3, 5, 5));
//SET CONTROLS ON BUTTONS PANEL
buttons.add(CalculateBtn);
buttons.add(Amortize);
buttons.add(ClrBtn);
buttons.add(CloseBtn);
//ADD ACTION LISTENER
CalculateBtn.addActionListener(this);
ClrBtn.addActionListener(this);
CloseBtn.addActionListener(this);
Amortize.addActionListener(this);
rateTermList.addActionListener(this);
//ADD PANELS
pane.add(keypad, BorderLayout.NORTH);
pane.add(buttons, BorderLayout.SOUTH);
pane.add(scroll, BorderLayout.CENTER);
addWindowListener(new WindowAdapter()
{
publicvoid windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
}
publicvoid actionPerformed(ActionEvent e)
{
String arg = lcdLoanAmt.getText();
int combined = Integer.parseInt(arg);
if (e.getSource() == CalculateBtn)
{
try
{
JOptionPane.showMessageDialog(null,"Got try here","Error", JOptionPane.ERROR_MESSAGE);
}
catch(NumberFormatException ev)
{
JOptionPane.showMessageDialog(null,"Got here","Error", JOptionPane.ERROR_MESSAGE);
}
}
if ((e.getSource() == CalculateBtn) && (arg !=null))
{
try{
if ((e.getSource() == CalculateBtn) && (rateTermList.getSelectedIndex() == 1))
{
monthlyInterest = interest[0] / (12 * 100);
termInMonths = term[0] * 12;
monthlyPayment = combined * (monthlyInterest / (1 - (Math.pow (1 + monthlyInterest, -termInMonths))));
lcdMonthlyPmt.setText(calcPattern.format(monthlyPayment));
}
if ((e.getSource() == CalculateBtn) && (rateTermList.getSelectedIndex() == 2))
{
monthlyInterest = interest[1] / (12 * 100);
termInMonths = term[1] * 12;
monthlyPayment = combined * (monthlyInterest / (1 - (Math.pow (1 + monthlyInterest, -termInMonths))));
lcdMonthlyPmt.setText(calcPattern.format(monthlyPayment));
}
if ((e.getSource() == CalculateBtn) && (rateTermList.getSelectedIndex() == 3))
{
monthlyInterest = interest[2] / (12 * 100);
termInMonths = term[2] * 12;
monthlyPayment = combined * (monthlyInterest / (1 - (Math.pow (1 + monthlyInterest, -termInMonths))));
lcdMonthlyPmt.setText(calcPattern.format(monthlyPayment));
}
}
catch(NumberFormatException ev)
{
JOptionPane.showMessageDialog(null,"Invalid Entry!\nPlease Try Again","Error", JOptionPane.ERROR_MESSAGE);
}
}
//IF STATEMENTS FOR AMORTIZATION
if ((e.getSource() == Amortize) && (rateTermList.getSelectedIndex() == 1))
{
loopy(7, 5.35);
}
if ((e.getSource() == Amortize) && (rateTermList.getSelectedIndex() == 2))
{
loopy(15, 5.5);
}
if ((e.getSource() == Amortize) && (rateTermList.getSelectedIndex() == 3))
{
loopy(30, 5.75);
}
if (e.getSource() == ClrBtn)
{
rateTermList.setSelectedIndex(0);
lcdLoanAmt.setText(null);
lcdMonthlyPmt.setText(null);
displayArea.setText(null);
}
if (e.getSource() == CloseBtn)
{
System.exit(0);
}
}
privatevoid loopy(int lTerm,double lInterest)
{
double total, monthly, monthlyrate, monthint, monthprin, balance, lastint, paid;
int amount, months, termloop, monthloop;
String lcd2 = lcdLoanAmt.getText();
amount = Integer.parseInt(lcd2);
termloop = 1;
paid = 0.00;
monthlyrate = lInterest / (12 * 100);
months = lTerm * 12;
monthly = amount *(monthlyrate/(1-Math.pow(1+monthlyrate,-months)));
total = months * monthly;
balance = amount;
while (termloop <= lTerm)
{
displayArea.setCaretPosition(0);
displayArea.append("\n");
displayArea.append("Year " + termloop +" of " + lTerm +": payments\n");
displayArea.append("\n");
displayArea.append("Month\tMonthly\tPrinciple\tInterest\tBalance\n");
monthloop = 1;
while (monthloop <= 12)
{
monthint = balance * monthlyrate;
monthprin = monthly - monthint;
balance -= monthprin;
paid += monthly;
displayArea.setCaretPosition(0);
displayArea.append(monthloop +"\t" + calcPattern.format(monthly) +"\t" + calcPattern.format(monthprin) +"\t");
displayArea.append(calcPattern.format(monthint) +"\t" + calcPattern.format(balance) +"\n");
monthloop ++;
}
termloop ++;
}
}
publicstaticvoid main(String args[])
MORT_RETRY f =new MORT_RETRY();
f.setTitle("MORTGAGE PAYMENT CALCULATOR");
f.setBounds(600, 600, 500, 500);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}
need help with displaying the textarea correctly and the scroll bar please.
Message was edited by:
new2this2020

