• 0

[Java] I need some help with JComboBox


Question

For my class assignment, we are making a mortgage calculator. Our first assignment allowed the user to enter the amount, the term, and the interest. For this assignment, we are to use the JComboBox to allow the user to select from a 7, 15, and 30 year loan, and the interest rates are to be associated with each selection.

I am not quite sure how to assign the interest rate to the term selections, and how to pass that value on for calculation. We are also suppose to have a table show the entire loan with monthly payments, interest rates, and balance after each month. I am more concerned with getting the combo box working for now. If anyone can give me any advice I would greatly appreciate it.

Here is the section of code from my last program for the calculations. The term select portion is what I need to change, as well as configure the combo box. Let me know if you guys need more from me.

private void computePayActionPerformed(java.awt.event.ActionEvent evt) {//event_computePayActionPerformed

	// calculate the monthly payment
	double p=Double.parseDouble(amount.getText());
	double r=Double.parseDouble(rate.getText())/100;
	double n=Integer.parseInt(term.getText());
	// this formula was borrowed from Desiree
	double monthPayment = ((p * r / 12 * Math.pow(r / 12 + 1,n*12))/(Math.pow(r/12+1,n*12)-1));
	DecimalFormat df = new DecimalFormat("$###,###.00");
	display.setText(df.format(monthPayment));

Link to comment
Share on other sites

17 answers to this question

Recommended Posts

  • 0

Oh, sorry about that, I guess I wasn't too specific. Basically the combo box should have three selections, 7, 15, and 30. Each of the choices needs to have a specific interest rate attached. Basically the user enters in the loan amount into the text field. Then the user chooses the term of the loan from the drop box (7, 15, 30). The specific interest rate associated with the selection should show up in the interest rate field. Then the user hits the calculate button and a monthly payment is displayed.

This program also requires an amortization table to show the entire loan by month, with loan amount, interest for that month and the remaining balance.

I can give a screen shot if that would help things out.

Link to comment
Share on other sites

  • 0

Create a HashMap. Use the legal values of Term as the keys, and the interest rates as the values. When the user changes the selection in the dropdown list you can query the currently selected value of the dropdown list, then use that to get the corresponding interest rate from the HashMap, and use that to update the value of the interest rate field.

All the details of how to do those things are in the Java API documentation.

Link to comment
Share on other sites

  • 0

Hey Steve.

I'm still having troubles compiling the code you sent me in VS2005.

I actually jumped on here to post a question, happened to see your thread, thought I'd just post in here, get some advice.

Steve (dub40) built the program in Netbeans. I'm trying to compile in VS2005, but get this error

But I still get "error VJS1161: Cannot find type 'javax.swing.JFrame'".

This is the only error I get.

I do know that he has included

import javax.swing.*;
import javax.swing.event.*;

I haven't actually done any Java programming since 1997, when it was still Java 1.2, so a lot of this code has me lost, but I figure I can figure out what he needs if I can get it to compile.

Any words of wisdom?

Link to comment
Share on other sites

  • 0
J# reads Java.

Yes, but J# isn't Java. I was thrown off by his screen shot and the Java icon in his form. It made me scratch my head and wonder if there was an add-in that allowed you to compile Java to the JVM.

Link to comment
Share on other sites

  • 0
J# reads Java.

The J# language (now end-of-lifed by MS) is reasonably syntax-compatible with real Java, but don't expect the real Sun Java API - hence the lack of Swing classes.

Time to get JDK 1.6.0.4 + Eclipse.

Link to comment
Share on other sites

  • 0

It appears that this is going to be more difficult than I thought. I am going to try to do some more research on how to get this project going. Thanks for all the replies guys.

Link to comment
Share on other sites

  • 0
It appears that this is going to be more difficult than I thought. I am going to try to do some more research on how to get this project going. Thanks for all the replies guys.

Shouldn't be more difficult. Create a class/struct that encapsulates Year and Rate. Bind the combobox to a list of that class. On selected index change, get the new interest rate.

I don't have VS 2005 installed, and Visual J# is no longer part of Visual Studio as of 2008. I'll have to install it when I get home to work up an example. VJ# is kind of quirky when it comes to .NET. I'm having problems with it in VS 2003.

Link to comment
Share on other sites

  • 0

Make a class to associate the year with the rate. Override ToString to return the year, as it will be called by the combo box.

public class YearRate
{
private String _year;
private double _rate;

public YearRate(String year, double rate)
{
  _year = year;
  _rate = rate;
}

public String get_Year()
{
  return _year;
}

public double get_Rate()
{
  return _rate;
}

public String ToString()
{
  return _year;
}
}

Use a vector to store the list of year/rates.

private Vector items;

...

items = new Vector();

items.add(new YearRate("7", 5.25));

items.add(new YearRate("15", 5.35));

items.add(new YearRate("30", 5.525));

cboBox = new JComboBox(items);

In the combo box's action listener, get a reference to the sender, and then use the combo box's selected item to reference the YearRate instance.

Object evtSrc = evt.getSource();

if (evtSrc.equals(cboBox))

{

YearRate yr = (YearRate)cboBox.getSelectedItem();

this.setTitle("Interest rate: " + Double.toString(yr.get_Rate())); // or in your case you'd set the text of your interest rate text box

}

Link to comment
Share on other sites

  • 0
Well done azcodemonkey. Nice to see some real coding without all that namby-pamby separation of UI and business logic. :)

Smart ass. :p LOL It ain't pretty, but it works. He could easily take the concept and abstract it properly. I'm not writing his assignment for him. I'm showing him that it isn't that difficult to find a solution. And screw using J# for swing. :x Get Netbeans!

Link to comment
Share on other sites

  • 0

Thank you for trying to help me out with my project, I really appreciate it. Unfortunately, this stuff is way over my head, and I don't understand any of it. I don't know why they require us to have such an advanced java class without much prior experience. I guess I'll have to figure out what to do to make it through this class.

Link to comment
Share on other sites

  • 0
Smart ass. :p LOL It ain't pretty, but it works. He could easily take the concept and abstract it properly. I'm not writing his assignment for him. I'm showing him that it isn't that difficult to find a solution. And screw using J# for swing. :x Get Netbeans!

I wasn't too stressed about it. I'm not a Java programmer (I work mainly in web coding [CFML, XML/XHTML, Perl] and C++/C# -- I wasn't going to install a new prog.

AFAIK, Steve (dub40) is using Netbeans.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.