9 Replies - 648 Views - Last Post: 01 March 2009 - 07:36 PM Rate Topic: -----

#1 corbymaker  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 01-March 09

help on ComboBox and amortization

Posted 01 March 2009 - 05:45 PM

I am working on my mortgage calculator but cannot seem to get the radio buttons to work correctly. any help or suggestions?


//imports required packages
import java.awt.*;					  //to create buttons
import javax.swing.*;				   //imports java swing
import java.awt.event.*;  //cover any action or item events and listeners
import java.text.DecimalFormat;		 //to import decimal format for money

class mortgageCalculatorMcCabe3 extends JFrame implements ActionListener, ItemListener {

	//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"};
	
	// create the comboboxes for class-wide use.
	 JComboBox termList = new JComboBox(ArrayTerm);
	 JComboBox interestRateList = new JComboBox(ArrayInterestRate);

mortgageCalculatorMcCabe3() {
	
	//Sets the title at the top of program 
	super("Ryan's Mortgage Calculator");	
	
	
//	Panel comboBox1 = new Panel();
	JComboBox interestRateList = null;
	
//	Panel comboBox2 = new Panel();
	JComboBox termList = null;
	   
	//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);

	 termList.setSelectedIndex(0);  // MOVED statements here
	 termList.addItemListener(this);
	 row2.add(termList);   // Add combo to row2 (temporarily)

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

	 interestRateList.setSelectedIndex(0); // MOVED statements here
	 interestRateList.addItemListener(this);
	 row3.add(interestRateList); // Add combo to row3

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

public void itemStateChanged( ItemEvent event )
{
	 if( event.getSource() == termList
			   && event.getStateChange() == ItemEvent.SELECTED )
	 {
		  termList.setSelectedIndex( termList.getSelectedIndex() );
	 }
	 else if( event.getSource() == interestRateList
			   && event.getStateChange() == ItemEvent.SELECTED )
	 {
		  interestRateList.setSelectedIndex( interestRateList.getSelectedIndex() );
	 }
}
//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



*Edited to add the [ code] [ /code] tags
And where are you RadioButtons ?
Please
:code:
it makes our life a lot easier

This post has been edited by pbl: 01 March 2009 - 07:37 PM


Is This A Good Question/Topic? 0
  • +

Replies To: help on ComboBox and amortization

#2 BigAnt  Icon User is offline

  • May Your Swords Stay Sharp
  • member icon

Reputation: 101
  • View blog
  • Posts: 2,392
  • Joined: 16-August 08

Re: help on ComboBox and amortization

Posted 01 March 2009 - 05:52 PM

Please edit your code to make proper use of the code tags: :code:

Also where do you do the radio buttons in your code?
Do you mean the combo box?
Was This Post Helpful? 0
  • +
  • -

#3 Nat3TheGreat13  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 162
  • Joined: 09-February 09

Re: help on ComboBox and amortization

Posted 01 March 2009 - 05:53 PM

You don't have any radio buttons
Was This Post Helpful? 0
  • +
  • -

#4 corbymaker  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 01-March 09

Re: help on ComboBox and amortization

Posted 01 March 2009 - 06:04 PM

//imports required packages
import java.awt.*;					  //to create buttons
import javax.swing.*;				   //imports java swing
import java.awt.event.*;  //cover any action or item events and listeners
import java.text.DecimalFormat;		 //to import decimal format for money

class mortgageCalculatorMcCabe3 extends JFrame implements ActionListener, ItemListener {

	//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"};
	
	// create the comboboxes for class-wide use.
	 JComboBox termList = new JComboBox(ArrayTerm);
	 JComboBox interestRateList = new JComboBox(ArrayInterestRate);

mortgageCalculatorMcCabe3() {
	
	//Sets the title at the top of program 
	super("Ryan's Mortgage Calculator");	
	
	
//	Panel comboBox1 = new Panel();
	JComboBox interestRateList = null;
	
//	Panel comboBox2 = new Panel();
	JComboBox termList = null;
	   
	//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);

	 termList.setSelectedIndex(0);  // MOVED statements here
	 termList.addItemListener(this);
	 row2.add(termList);   // Add combo to row2 (temporarily)

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

	 interestRateList.setSelectedIndex(0); // MOVED statements here
	 interestRateList.addItemListener(this);
	 row3.add(interestRateList); // Add combo to row3

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

public void itemStateChanged( ItemEvent event )
{
	 if( event.getSource() == termList
			   && event.getStateChange() == ItemEvent.SELECTED )
	 {
		  termList.setSelectedIndex( termList.getSelectedIndex() );
	 }
	 else if( event.getSource() == interestRateList
			   && event.getStateChange() == ItemEvent.SELECTED )
	 {
		  interestRateList.setSelectedIndex( interestRateList.getSelectedIndex() );
	 }
}
//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




I meant combo boxes not radio buttons, sorry
Was This Post Helpful? 0
  • +
  • -

#5 Nat3TheGreat13  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 162
  • Joined: 09-February 09

Re: help on ComboBox and amortization

Posted 01 March 2009 - 06:11 PM

What exactly do you want to do with them? I got them to actually show up now.
//imports required packages
import java.awt.*; //to create buttons
import javax.swing.*; //imports java swing
import java.awt.event.*; //cover any action or item events and listeners
import java.text.DecimalFormat; //to import decimal format for money

class mortgageCalculatorMcCabe3 extends JFrame implements ActionListener, ItemListener {

//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"};

// create the comboboxes for class-wide use.
JComboBox termList = new JComboBox(ArrayTerm);
JComboBox interestRateList = new JComboBox(ArrayInterestRate);

mortgageCalculatorMcCabe3() {

//Sets the title at the top of program
super("Ryan's Mortgage Calculator");


// Panel comboBox1 = new Panel();


// Panel comboBox2 = new Panel();


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

termList.setSelectedIndex(0); // MOVED statements here
termList.addItemListener(this);
row2.add(termList); // Add combo to row2 (temporarily)

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

interestRateList.setSelectedIndex(0); // MOVED statements here
interestRateList.addItemListener(this);
row3.add(interestRateList); // Add combo to row3

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

}

public void itemStateChanged( ItemEvent event )
{
if( event.getSource() == termList
&& event.getStateChange() == ItemEvent.SELECTED )
{
termList.setSelectedIndex( termList.getSelectedIndex() );
}
else if( event.getSource() == interestRateList
&& event.getStateChange() == ItemEvent.SELECTED )
{
interestRateList.setSelectedIndex( interestRateList.getSelectedIndex() );
}
}
//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 newmortgageCalculatorMcCabe3 = new mortgageCalculatorMcCabe3 ();
}
}//end program


Was This Post Helpful? 0
  • +
  • -

#6 corbymaker  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 01-March 09

Re: help on ComboBox and amortization

Posted 01 March 2009 - 06:13 PM

I am trying to get it so when I select the 7 from the drop down box, the 5.35 is selected in the interest rate field and so on for all 3. then get the monthly payment to be calculated on those select.
Was This Post Helpful? 0
  • +
  • -

#7 Nat3TheGreat13  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 162
  • Joined: 09-February 09

Re: help on ComboBox and amortization

Posted 01 March 2009 - 06:46 PM

Why do you have the Textfields if you want the user to use the combo boxes?
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: help on ComboBox and amortization

Posted 01 March 2009 - 07:28 PM

So should we change the topic title from "Help on Radio Button and Amortzation" to "Help on ComboBox and Amortzation" ?
Was This Post Helpful? 0
  • +
  • -

#9 corbymaker  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 01-March 09

Re: help on ComboBox and amortization

Posted 01 March 2009 - 07:34 PM

yes, but how do I do that?
Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: help on ComboBox and amortization

Posted 01 March 2009 - 07:36 PM

View Postcorbymaker, on 1 Mar, 2009 - 06:34 PM, said:

yes, but how do I do that?

You can Edit (full Edit) your first post but I'll do it for you
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1