I don't know what I;m doing wrong. It compiles fine but when I run and hit the "Calculate" button, nothing is displayed in TextArea and I get an error. ????
CODE
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.*;
public class ModifiedFutureValueApp
{
public static void main(String[] args)
{
JFrame frame = new FutureValueFrame();
frame.setVisible(true);
}
}
class FutureValueFrame extends JFrame
{
public FutureValueFrame()
{
setTitle("Future Value Calculator");
centerWindow(this);
setSize(400, 400);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new FutureValuePanel();
getContentPane().add(panel);
}
private void centerWindow(Window w)
{
Toolkit tk = Toolkit.getDefaultToolkit();
Dimension d = tk.getScreenSize();
setLocation((d.width-w.getWidth())/2, (d.height-w.getHeight())/2);
}
}
class FutureValuePanel extends JPanel implements ActionListener
{
private JTextField paymentTextField,
rateTextField;
private JTextArea futureValueTextArea;
private JScrollPane scrollPane;
private JLabel paymentLabel,
rateLabel,
yearsLabel,
futureValueLabel;
private JButton calculateButton,
exitButton;
private JComboBox yearsComboBox;
public FutureValuePanel()
{
setLayout(new GridBagLayout());
// payment label
paymentLabel = new JLabel("Monthly Payment:");
add(paymentLabel, getConstraints(0,0,1,1, GridBagConstraints.EAST));
// payment text field
paymentTextField = new JTextField(15);
add(paymentTextField, getConstraints(1,0,1,1, GridBagConstraints.WEST));
// rate label
rateLabel = new JLabel("Yearly Interest Rate:");
add(rateLabel, getConstraints(0,1,1,1, GridBagConstraints.EAST));
// rate text field
rateTextField = new JTextField(15);
add(rateTextField, getConstraints(1,1,1,1, GridBagConstraints.WEST));
// years label
yearsLabel = new JLabel("Number of Years:");
add(yearsLabel, getConstraints(0,2,1,1, GridBagConstraints.EAST));
//years combo box
String[] years = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
"11", "12", "13", "14", "15", "16", "17", "18", "19", "20" };
yearsComboBox = new JComboBox(years);
yearsComboBox.setSelectedIndex(0);
yearsComboBox.addActionListener(this);
add(yearsComboBox, getConstraints(1,2,1,1, GridBagConstraints.WEST));
// future value label
futureValueLabel = new JLabel("Future Value:");
add(futureValueLabel, getConstraints(0,3,1,1, GridBagConstraints.EAST));
// future value text field
JTextArea futureValueTextArea = new JTextArea(5, 15);
JScrollPane scrollPane = new JScrollPane(futureValueTextArea,
ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
futureValueTextArea.setEditable(false);
add(scrollPane, getConstraints(1,3,1,1, GridBagConstraints.WEST));
// calculate button
calculateButton = new JButton("Calculate");
calculateButton.addActionListener(this);
add(calculateButton, getConstraints(1,4,1,1, GridBagConstraints.CENTER));
// exit button
exitButton = new JButton("Exit");
exitButton.addActionListener(this);
add(exitButton, getConstraints(2,4,1,1, GridBagConstraints.WEST));
}
public void actionPerformed(ActionEvent e)
{
Object source = e.getSource();
if (source == exitButton)
System.exit(0);
else if (source == calculateButton)
{
double payment = Double.parseDouble(paymentTextField.getText());
double rate = Double.parseDouble(rateTextField.getText());
int years = yearsComboBox.getSelectedIndex();
double futureValue = FinancialCalculations.calculateFutureValue(
payment, rate, years);
NumberFormat currency = NumberFormat.getCurrencyInstance();
String result = currency.format(futureValue);
futureValueTextArea.setText(result);
}
}
private GridBagConstraints getConstraints(int gridx, int gridy, int gridwidth
, int gridheight, int anchor)
{
GridBagConstraints c = new GridBagConstraints();
c.insets = new Insets(5, 5, 5, 5);
c.ipadx = 0;
c.ipady = 0;
c.gridx = gridx;
c.gridy = gridy;
c.gridwidth = gridwidth;
c.gridheight = gridheight;
c.anchor = anchor;
return c;
}
}
Thank you!