Hi, I'm having some difficultly figuring out how to put a value to my combobox items. For example I have 3 states listed in the combobox.

String[] stateStrings = {"Michigan", "Wisconsin", "Illinois"};
JComboBox jComboBox = new JComboBox (stateStrings);
jComboBox.setSelectedIndex(2);
 jPanel3.add(jComboBox);

I need to put different yearly interest rates or APR values on each. These rates are later put into the monthly payments formula:

int r=(p*(i/100))/(1-(Math.power((1+(i/100)),(n*(-1)))));

after making APR = interest rate.

My question is how do I accomplish this. I also would like some help on how to make my try and catch statements work for the better. I tried toying around with it but I'm having difficulty understanding the concept. I really caught on to Visual Basics fairly easy than this and I can't understand how.

For example:

public void actionPerformed(ActionEvent e)
	{
	try
		{
			
			int principle=Integer.parseInt(jTextField1.getText()), downPayment=Integer.parseInt(jTextField2.getTest()), temp; 
			resultArea.setText("");
			// This subtracts the  downPayment from the principle
			if (p<b)
///need the downpayment to be subtracted from the principle amount and catch error if anything is typed into each field other than a integer 
		}
    
		catch (Exception ex)
		{
			// If anything other than two integers are input, print out the error 

message
			resultArea.setText("Input House Price");
		
			
		}

Recommended Answers

All 5 Replies

As far as the error handling you are already doing that. If you enter something other than a number, then the method:
Integer.parseInt, will throw an exception and the code will "go" inside the catch. You don't need to check anything:

public void actionPerformed(ActionEvent e)
	{
	try   {
    int principle=Integer.parseInt(jTextField1.getText());
    int downPayment=Integer.parseInt(jTextField2.getText());
// if the above values are not numbers then the program will take you automatically to the catch.

    int temp = 0;
    resultArea.setText("");		
			if (p<b)  {
// do the calculations. If you made it this far, means that whatever you entered where integers.
                        }
		} catch (NumberFormatException ex) {
// The above exception is thrown when you don't enter a number as argument to the parseInt, parseFloat, .... methods

     System.out.println("Invalid Number");
	resultArea.setText("Input House Price");	
		} catch (Exception e) {
// Catch the above in case something else happens
     System.out.println("An error has occured: " + e.getMessage());
}
}

As for the ComboBox, the examples you posted seems to work. What are you trying to accomplish that doesn't work?

I wanted to link the combobox items to the APR or interest rates. There is a APR for each state and I was not sure of how to do it.
The APR rates are 6.7, 7.2, and 5.3.

I thought that I could create an array and then link it like that but I am not sure.. for instance

double[] APR = {6.7, 7.2, 5.3};

and where I go from here is a mystery to me.

Did you look the API of the JComboBox. Do that and you will find a constructor that you can use.

Also, when you create a JComboBox using a String array, there is no constructor that takes as a parameter a String array, there is something else.

Take a look at the constructors and then post that you think you should do.

Thanks for your help. I really appreciate it, you have really set me on the right path.I have just really had a lot of trouble understanding the whole coding in Java.I had experience with Visual Basics but it is more GUI than anything. When it comes to constructors there are multiple options with Java and choosing the right one becomes kind of confusing. But I will have to practice. Practice makes perfect.

Much appreciated.

Thanks for your help. I really appreciate it, you have really set me on the right path.I have just really had a lot of trouble understanding the whole coding in Java.I had experience with Visual Basics but it is more GUI than anything. When it comes to constructors there are multiple options with Java and choosing the right one becomes kind of confusing. But I will have to practice. Practice makes perfect.

Much appreciated.

Which one did you use and why. Also what method are you using in order to get the data from the selected Item of the Combo Box

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.