Skip to content

Instantly share code, notes, and snippets.

@bartenbach
Created November 29, 2011 22:32
Show Gist options
  • Save bartenbach/1406897 to your computer and use it in GitHub Desktop.
Save bartenbach/1406897 to your computer and use it in GitHub Desktop.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package futurevalueapp;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.NumberFormat;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
/**
*
* @author SeeD419
*/
public class FutureValuePanel extends JPanel implements ActionListener {
private JTextField paymentTextField,
rateTextField,
yearsTextField,
futureValueTextField;
private JLabel paymentLabel,
rateLabel,
yearsLabel,
futureValueLabel;
private JButton calculateButton,
exitButton;
private JComboBox yearsComboBox;
public FutureValuePanel()
{
// display panel
JPanel displayPanel = new JPanel();
displayPanel.setLayout(new GridBagLayout());
// payment label
paymentLabel = new JLabel("Monthly Payment:");
displayPanel.add(paymentLabel, getConstraints(0, 0, 1, 1, GridBagConstraints.WEST));
// payment text field
paymentTextField = new JTextField(10);
displayPanel.add(paymentTextField, getConstraints(0, 1, 1, 1, GridBagConstraints.EAST));
// rate label
rateLabel = new JLabel("Yearly Interest Rate:");
displayPanel.add(rateLabel, getConstraints(1, 0, 1, 1, GridBagConstraints.WEST));
// rate text field
rateTextField = new JTextField(10);
displayPanel.add(rateTextField, getConstraints(1, 1, 1, 1, GridBagConstraints.EAST));
// years label
yearsLabel = new JLabel("Number of Years:");
displayPanel.add(yearsLabel, getConstraints(2, 0, 1, 1, GridBagConstraints.WEST));
// years text field
//yearsTextField = new JTextField(10);
//displayPanel.add(yearsTextField);
Integer 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);
displayPanel.add(yearsComboBox, getConstraints(2, 1, 1, 1, GridBagConstraints.EAST));
// future value label
futureValueLabel = new JLabel("Future Value:");
displayPanel.add(futureValueLabel, getConstraints(3, 0, 1, 1, GridBagConstraints.WEST));
// future value text field
futureValueTextField = new JTextField(10);
futureValueTextField.setEditable(false);
futureValueTextField.setFocusable(false);
displayPanel.add(futureValueTextField, getConstraints(3, 1, 1, 1, GridBagConstraints.EAST));
// button panel
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridBagLayout());
// calculate button
calculateButton = new JButton("Calculate");
calculateButton.addActionListener(this);
buttonPanel.add(calculateButton, getConstraints(4, 0, 1, 1, GridBagConstraints.WEST));
// exit button
exitButton = new JButton("Exit");
exitButton.addActionListener(this);
buttonPanel.add(exitButton, getConstraints(4, 1, 1, 1, GridBagConstraints.EAST));
// add panels to main panel
this.setLayout(new BorderLayout());
this.add(displayPanel, BorderLayout.CENTER);
this.add(buttonPanel, BorderLayout.SOUTH);
}
@Override
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 = Integer.parseInt(yearsTextField.getText());
double futureValue = FinancialCalculations.calculateFutureValue(
payment, rate, years);
NumberFormat currency = NumberFormat.getCurrencyInstance();
futureValueTextField.setText(currency.format(futureValue));
}
}
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 = gridy;
c.gridy = gridx;
c.gridwidth = gridwidth;
c.gridheight = gridheight;
c.anchor = anchor;
return c;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment