-
ComboBox help
I am trying to add a drop down combo box for my program but I am having trouble adding items to the combo box. can anyone tell me what i am doing wrong or what I need to add/change.
Code:
//imports required packages
import java.awt.*; //to create buttons
import javax.swing.*; //imports java swing
import java.awt.event.ActionEvent; //imports event action events
import java.awt.event.ActionListener; //imports event action listener for the event
import java.text.DecimalFormat; //to import decimal format for money
class mortgageCalculatorMcCabe3 extends JFrame implements ActionListener {
//Fields for user input
JTextField principalAmount = new JTextField(15);
JTextField yearlyInterestRate = new JTextField(15);
JTextField numberOfYears = new JTextField(15);
JTextField monthlyPayments = new JTextField(15);
JLabel validateLabel = new JLabel();
public JComboBox MortIntRate;
/*Declaring and initializing variables*/
int term = 0;
double principleAmount = 0;
String ArrayTerm [] = {"7", "15", "30"};
String ArrayInterestRate [] = {"5.35", "5.5", "5.75"};
mortgageCalculatorMcCabe3() {
//Sets the title at the top of program
super("Ryan's Mortgage Calculator");
Panel comboBox = new Panel();
JComboBox interestRateList, termList;
//Sets the size of the Frame
setSize(350, 225);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container pane = getContentPane();
FlowLayout flow = new FlowLayout(FlowLayout.RIGHT);
pane.setLayout(flow);
//Creates Labels and Fields for row 1
JPanel row1 = new JPanel();
JLabel principalLabel = new JLabel("Loan Amount:");
row1.add(principalLabel);
row1.add(principalAmount);
pane.add(row1);
//Creates Labels and Fields for row 2
JPanel row2 = new JPanel();
JLabel numYearsLabel = new JLabel("Term of Loan(Years):");
row2.add(numYearsLabel);
row2.add(numberOfYears);
pane.add(row2);
////Creates Labels and Fields for row 3
JPanel row3 = new JPanel();
JLabel yearIntrateLabel = new JLabel("Interest Rate:");
row3.add(yearIntrateLabel);
row3.add(yearlyInterestRate);
pane.add(row3);
////Creates Labels and Fields for row 4
JPanel row4 = new JPanel();
JLabel monthlyPaymentsLabel = new JLabel("Total Monthly Payment:");
row4.add(monthlyPaymentsLabel);
row4.add(monthlyPayments);
pane.add(row4);
setContentPane(pane);
setVisible(true);
//Creates Labels and Fields for row 5
JPanel row5 = new JPanel();
JButton calculate = new JButton("Calculate");
calculate.addActionListener(this);
row5.add(calculate);
pane.add(row5);
JButton reset = new JButton("Clear");
reset.addActionListener(this);
row5.add(reset);
pane.add(row5);
JButton exit = new JButton("Exit");
exit.addActionListener(this);
row5.add(exit);
pane.add(row5);
termList = new JComboBox(ArrayTerm);
termList.setSelectedIndex(0);
termList.addItemListener(this);
interestRateList = new JComboBox(ArrayInterestRate);
interestRateList.setSelectedIndex(0);
interestRateList.addItemListener(this);
/*Setup list*/
comboBox.add(loanTerm);
comboBox.add(termList);
comboBox.add(loanInterest);
comboBox.add(interestRateList);
}
//This parts performs the Actions for the Calculate, Clear & Exit buttons
public void actionPerformed(ActionEvent event) {
String command = event.getActionCommand();
if (command == "Calculate")
compute();
if (command == "Clear")
clearAllFields();
if (command == "Exit")
exit();}
void compute()
{
try{
validateLabel.setText("");
validateLabel.validate();
Double yearIntrate = new Double (yearlyInterestRate.getText());
Double principal = new Double (principalAmount.getText());
Integer numYears = new Integer (numberOfYears.getText());
double payFreq = 12;
double interestRateMonths;
double monthlyPayment;
double numPayments;
//results and formula's
interestRateMonths = (yearIntrate.doubleValue() / 100) / payFreq;
numPayments = payFreq * numYears.intValue();
double temp = Math.pow(1 + interestRateMonths, (-1*numPayments));
monthlyPayment = principal.doubleValue() * interestRateMonths / (1 - (temp));
//Format used to set two decimal places.
DecimalFormat currency = new DecimalFormat("$0.00");
monthlyPayments.setText(currency.format(monthlyPayment));
//if user submit non valid information it provides an error.
} catch(Exception error)
{
validateLabel.setText("Invalid Entry! Try again.");
}
}
//to clear the fields back to beginning
void clearAllFields(){
principalAmount.setText("");
yearlyInterestRate.setText("");
numberOfYears.setText("");
monthlyPayments.setText("");
}
//exit program
void exit(){
System.exit(0);
}
public static void main(String[] arguments)
{
mortgageCalculatorMcCabe3 mortgageCalculatorMcCabe3 = new mortgageCalculatorMcCabe3 ();
}
}//end program
Last edited by Hack; 02-26-2009 at 09:49 AM.
Reason: Added Code Tags
-
What did you do to add items, and what error did you get?
Similar Threads
-
By diana_j86 in forum .NET
Replies: 5
Last Post: 04-06-2009, 01:46 PM
-
By subrama6 in forum .NET
Replies: 4
Last Post: 02-13-2008, 09:19 AM
-
By qvqnytowl in forum VB Classic
Replies: 1
Last Post: 08-28-2006, 06:51 PM
-
By Stan Shankman in forum .NET
Replies: 2
Last Post: 09-07-2001, 03:06 AM
-
By xiao_john@yahoo.com in forum VB Classic
Replies: 0
Last Post: 02-20-2001, 01:23 AM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks