What I would like to do is to add a dropdown menu instead of having the buttons for the three loans. I tried a few different things but just cannot figure out how to do this and get it to work. Please can someone help? Here is my code with the buttons:
// import of the packages
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.*;
public class RSpencerMortCalcV2 extends JFrame implements ActionListener
{
// variables declaration
private JLabel labelamt, labelMonthlyPay, labeltitle, labelAmortTable, labelTerm, labelRate;
private JButton button7, button15, button30, buttonReset, buttonQuit, buttonCalc;
private JTextField textFieldAmount, textFieldMonthlyPayment, textFieldyears, textFieldinterest;
private JComboBox jcb;
private JTextArea taAmortTable;
private JPanel contentPane;
double interest, principle, bal, monthlyPay, Amount, Interest;
int[] Years = {7, 15, 30};
double[] Rate = {5.35, 5.50, 5.75};
double Amt = Amount;
int years;
// end of variables declaration
public RSpencerMortCalcV2()
{
super();
initializeComponent();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
pack();
// add listeners
button7.addActionListener(this);
button15.addActionListener(this);
button30.addActionListener(this);
buttonCalc.addActionListener(this);
buttonReset.addActionListener(this);
buttonQuit.addActionListener(this);
}
public void initializeComponent()
{
// initialize labels and text fields
labelamt = new JLabel(" Amount");
labelMonthlyPay = new JLabel(" Monthly Payment");
labeltitle = new JLabel("Mortgage Calculator");
labelAmortTable = new JLabel("Amortization Table");
labelTerm = new JLabel(" Years");
labelRate = new JLabel(" Interest Rate");
textFieldAmount = new JTextField("", 6);
textFieldMonthlyPayment = new JTextField("", 8);
textFieldyears = new JTextField("", 12);
textFieldinterest = new JTextField("", 12);
taAmortTable = new JTextArea(10, 300);
JScrollPane scrollP = new JScrollPane(taAmortTable, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
// initialize buttons
button7 = new JButton("7 years at 5.35%");
button7.setActionCommand("Calc7");
button15 = new JButton("15 years at 5.5%");
button15.setActionCommand("Calc15");
button30 = new JButton("30 years at 5.75%");
button30.setActionCommand("Calc30");
buttonCalc = new JButton("Calculate");
buttonCalc.setActionCommand("Calc");
buttonReset = new JButton("Reset");
buttonReset.setActionCommand("Reset");
buttonQuit = new JButton("Quit");
buttonQuit.setActionCommand("Quit");
// set object attributes
textFieldMonthlyPayment.setEditable(false);
taAmortTable.setEditable(false);
Container contentPane = getContentPane();
// add the components to the panel
contentPane.setLayout(null);
addComponent(contentPane, labeltitle, 150,20,500,15);
addComponent(contentPane, labelamt, 130,60,80,25);
addComponent(contentPane, labelMonthlyPay, 130,220,100,25);
addComponent(contentPane, labelAmortTable, 180,250,300,25);
addComponent(contentPane, labelTerm, 130,85,80,40);
addComponent(contentPane, labelRate, 130,125,80,25);
addComponent(contentPane, textFieldAmount, 240,60,100,25);
addComponent(contentPane, textFieldMonthlyPayment, 240,220,100,25);
addComponent(contentPane, textFieldyears, 240,90,100,25);
addComponent(contentPane, textFieldinterest, 240,125,100,25);
addComponent(contentPane, button7, 35,170,130,30);
addComponent(contentPane, button15, 180,170,130,30);
addComponent(contentPane, button30, 320,170,135,30);
addComponent(contentPane, scrollP, 50,280,400,270);
addComponent(contentPane, buttonCalc, 50,570,100,30);
addComponent(contentPane, buttonReset, 170,570,95,30);
addComponent(contentPane, buttonQuit, 290,570,95,30);
}
public static void main(String[] args)
{
JFrame window = new RSpencerMortCalcV2();
window.setTitle("Mortgage Calculator");
window.setLocation(new Point(0, 0));
window.setSize(new Dimension(500, 700));
}
// add component without a layout manager (Absolute Positioning)
private void addComponent(Container container,Component c, int x, int y, int width, int height)
{
c.setBounds(x,y,width,height);
container.add(c);
}
public void actionPerformed (ActionEvent e)
{
//perform action based on with button was pressed
if ("Calc7".equals(e.getActionCommand()))
{
DecimalFormat decimalPlaces = new DecimalFormat("0.00");
NumberFormat currency = NumberFormat.getCurrencyInstance();
try // to validate user input of amount field
{
Amt = Double.parseDouble(textFieldAmount.getText());
}
catch(NumberFormatException event)
{
JOptionPane.showMessageDialog(null, "Enter Number Value Only for Amount!", "INVALID ENTRY", JOptionPane.ERROR_MESSAGE);
return; // plain old "return" since it's in actionPerformed
}
double payment = CalcPay7();
textFieldMonthlyPayment.setText("" + (currency.format(payment)));
taAmortTable.setText("");
taAmortTable.append("Payment \t Remaining \t\t Interest\n");
for(int i = 1; i <=(Years[0]*12); i++) // loop through monthly payment
{
bal=Amt; // set bal to loan
// calculations on monthly payment
interest = (((Rate[0]/100.0)/12.0)*bal); // monthly interest
principle = (payment-interest); // applied towards principle
Amt=(bal-principle); // loan balance after payment
taAmortTable.append(i + " \t " + currency.format(Amt) + " \t\t " + currency.format(interest) + "\n"); // add to amort table
} // end for
} // end first if for button press
else if ("Calc15".equals(e.getActionCommand()))
{
DecimalFormat decimalPlaces = new DecimalFormat("0.00");
NumberFormat currency = NumberFormat.getCurrencyInstance();
try // to validate user input of amount field
{
Amt = Double.parseDouble(textFieldAmount.getText());
}
catch(NumberFormatException event)
{
JOptionPane.showMessageDialog(null, "Enter Number Value Only for Amount!", "INVALID ENTRY", JOptionPane.ERROR_MESSAGE);
return; // plain old "return" since it's in actionPerformed
}
double payment = CalcPay15();
textFieldMonthlyPayment.setText("" + (currency.format(payment)));
taAmortTable.setText("");
taAmortTable.append("Payment \t Remaining \t\t Interest\n");
for(int i = 1; i <=(Years[1]*12); i++) // loop through monthly payment
{
bal=Amt; // set bal to loan
// calculations on monthly payment
interest = (((Rate[1]/100.0)/12.0)*bal); // monthly interest
principle = (payment-interest); // applied towards principle
Amt=(bal-principle); // loan balance after payment
taAmortTable.append(i + " \t " + currency.format(Amt) + " \t\t " + currency.format(interest) + "\n"); // add to amort table
} // end for
} // end first if for button press
else if ("Calc30".equals(e.getActionCommand()))
{
DecimalFormat decimalPlaces = new DecimalFormat("0.00");
NumberFormat currency = NumberFormat.getCurrencyInstance();
try // to validate user input of amount field
{
Amt = Double.parseDouble(textFieldAmount.getText());
}
catch(NumberFormatException event)
{
JOptionPane.showMessageDialog(null, "Enter Number Value Only for Amount!", "INVALID ENTRY", JOptionPane.ERROR_MESSAGE);
return; // plain old "return" since it's in actionPerformed
}
double payment = CalcPay30();
textFieldMonthlyPayment.setText("" + (currency.format(payment)));
taAmortTable.setText("");
taAmortTable.append("Payment \t Remaining \t\t Interest\n");
for(int i = 1; i <=(Years[2]*12); i++) // loop through monthly payment
{
bal=Amt; // set bal to loan
// calculations on monthly payment
interest = (((Rate[2]/100.0)/12.0)*bal); // monthly interest
principle = (payment-interest); // applied towards principle
Amt=(bal-principle); // loan balance after payment
taAmortTable.append(i + " \t " + currency.format(Amt) + " \t\t " + currency.format(interest) + "\n"); // add to amort table
} // end for
} // end first if for button press
else if("Calc".equals(e.getActionCommand()))
{
DecimalFormat decimalPlaces = new DecimalFormat("0.00");
NumberFormat currency = NumberFormat.getCurrencyInstance();
try // validate user input of all three text fields
{
Amt = Double.parseDouble(textFieldAmount.getText());
years = Integer.parseInt(textFieldyears.getText());
Interest = Double.parseDouble(textFieldinterest.getText());
}
catch(NumberFormatException event)
{
JOptionPane.showMessageDialog(null, "Enter Number Value Only for Amount, Years, and Interest!", "INVALID ENTRY", JOptionPane.ERROR_MESSAGE);
return; // plain old "return" since it's in actionPerformed
}
double payment = CalcPay(); // to calculate if user selected to input all three fields
textFieldMonthlyPayment.setText("" + (currency.format(payment)));
taAmortTable.setText("");
taAmortTable.append("Payment \t Remaining \t\t Interest\n");
for(int i = 1; i <=(years*12); i++) // for loop for amort table
{
bal=Amt; // set bal to loan
// calculations on monthly payment
interest = (((Interest/100.0)/12.0)*bal); // monthly interest
principle = (payment-interest); // applied towards principle
Amt=(bal-principle); // loan balance after payment
taAmortTable.append(i + " \t " + currency.format(Amt) + " \t\t " + currency.format(interest) + "\n"); // add to amort table
} // end for loop
} // end of calc else if
else if ("Reset".equals(e.getActionCommand()))
{
ResetFields(); // action command for reset button
}
else if ("Quit".equals(e.getActionCommand()))
{
System.exit(0); // action command for quit button
}
}
public double CalcPay7()
{
// perform calculations if input is valid
textFieldMonthlyPayment.setText("");
double MonthPay = (loan() * (Rate[0]/100/12)) / (1 - Math.pow(1/(1 + (Rate[0]/100/12)), Years[0]*12));
return MonthPay;
}
public double CalcPay15()
{
// perform calculations if input is valid
textFieldMonthlyPayment.setText("");
double MonthPay = (loan() * (Rate[1]/100/12)) / (1 - Math.pow(1/(1 + (Rate[1]/100/12)), Years[1]*12));
return MonthPay;
}
public double CalcPay30()
{
// perform calculations if input is valid
textFieldMonthlyPayment.setText("");
double MonthPay = (loan() * (Rate[2]/100/12)) / (1 - Math.pow(1/(1 + (Rate[2]/100/12)), Years[2]*12));
return MonthPay;
}
public double CalcPay()
{
// calculations for calc button
textFieldMonthlyPayment.setText("");
double MonthPay = (loan()* (Interest/100/12)) / (1 - Math.pow(1/(1 + (Interest/100/12)), years*12));
return MonthPay;
} // end of calc
public void ResetFields()
{
// set all fields to blank
textFieldAmount.setText("");
textFieldMonthlyPayment.setText("");
taAmortTable.setText("");
textFieldyears.setText("");
textFieldinterest.setText("");
}
public double loan()
{
double loan = Double.parseDouble(textFieldAmount.getText());
return loan;
}
}