Need help with JTextArea and Scrolling

I can't get the scroll bar to work. Here is my code.

import java.awt.*;

import java.awt.event.*;

import java.text.DecimalFormat;

import javax.swing.*;

public class MORT_RETRY extends JFrame implements 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();

//DEFINE TEXT FIELDS AND AREA

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()

{

public void windowClosing(WindowEvent e)

{

System.exit(0);

}// end window Closing

}// end Window Adapter

);

}// end MORT_RETRY

public void actionPerformed(ActionEvent e)

{

String arg = lcdLoanAmt.getText();

int combined = Integer.parseInt(arg);

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));

}//end if

//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);

}// end if ClrBtn

if (e.getSource() == CloseBtn)

{

System.exit(0);

}// end if CloseBtn

}// end actionPerformed

private void loopy(int lTerm,double lInterest)

{

double total, monthly, monthlyrate, monthint, monthprin, balance, lastint, paid;//declare the decimal variables

int amount, months, termloop, monthloop;//declare non-decimal variables

String lcd2 = lcdLoanAmt.getText();

amount = Integer.parseInt(lcd2); //loan Amount

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 ++;

}//end monthloop

termloop ++;

}//end termloop

}//end loopy

public static void main(String args[])

{

//JFrame.setDefaultLookAndFeelDecorated(true);

MORT_RETRY f = new MORT_RETRY();

f.setTitle("MORTGAGE PAYMENT CALCULATOR");

f.setBounds(600, 600, 500, 500);

f.setLocationRelativeTo(null);

f.setVisible(true);

}// end main

}//end class

I have been reading some of the forums help on the subject and have made it this far, but not I can't see the text area or scoll and the screen changes when I hit my amortize button. Please help.

Thank you.

[7205 byte] By [new2this2020a] at [2007-11-27 11:05:43]
# 1

Somebody plz help.

new2this2020a at 2007-7-29 13:10:34 > top of Java-index,Java Essentials,New To Java...
# 2

It's only been an hour for gosh sakes. Listen, if you want folks to read your code, please make it easy for them to read. Use code tags. If I were you, I'd repaste your code with the tags. Just paste the code from your compiler into the editor window here, highlight the code and click the "code" button above the editor window. Do that for each block of separate code you have (code separated by non code text). That will get you a quicker response.

Good luck

One more thing. You should probably post Swing-related questions in the Swing forum.

Message was edited by:

petes1234

petes1234a at 2007-7-29 13:10:34 > top of Java-index,Java Essentials,New To Java...
# 3

Some suggestions:

1) get your business logic separate from your GUI. It's hard to debug and figure out with both types of code bumping into each other here.

2) This is not good:

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);

}

Better and more concise to do something like:

if (e.getSource() == Amortize)

{

int indx = rateTermList.getSelectedIndex();

if (indx > 0)

{

loopy(term[indx - 1], interest[indx - 1]);

}

}

same goes for the calculate button code.

There are many more errors present, but these are two I saw on first glance.

Note my use of code tags and how it improves readability?

Message was edited by:

petes1234

petes1234a at 2007-7-29 13:10:34 > top of Java-index,Java Essentials,New To Java...
# 4

Thank you and sorry

new2this2020a at 2007-7-29 13:10:34 > top of Java-index,Java Essentials,New To Java...
# 5

A glaring error is on this line here:

keypad.add(displayArea);

You seem to be adding the "displayArea" JPanel to your display twice, once in the scroll, which is probably the correct location, and then again here in the keypad JPanel. When you try to add a component twice, only the second one shows up. Get rid of this line, and you may solve many of your problems.

Good luck.

Message was edited by:

petes1234

petes1234a at 2007-7-29 13:10:34 > top of Java-index,Java Essentials,New To Java...
# 6

That was it thank you so much

new2this2020a at 2007-7-29 13:10:34 > top of Java-index,Java Essentials,New To Java...