Hello. I'm having troubles with my JComboBox calculations. For example, if the strings in my JComboBox were String[] name = { "John", "Tom" }; And Tom was assigned 15% to his name. How can I have the cases in my action performed reach out to a calculation class to perform the calculation. What I want to do is have a total of the percent and amount added in a JTextfield to display in another JTextField. So basically the user is selecting a person, which has a certain percent assigned to the name depending on the person. Then the % is multiplied by a number from another textfield. The part I'm trying to figure out is how to display the total in another JTextfield depending on the person selected. If they pick case 0, it will only show John's amount in the totalField or if they pick Tom, it will only show Tom's amount in the totalField. What is the best way I can do this. I know how to do the calculations, it's the displaying part that I'm finding difficult. I left out alot of stuff, I'm just using this as an example. Thanks for hearing me out.

public void actionPerformed(ActionEvent e)
{
   if(e.getSource() == nameCombo)
		{
			switch(nameCombo.getSelectedIndex())
			{
				case 0:
					break;
				case 1:
					break;
				
			}

		}

}

 private void calc()
     {

		double dollarNumber = 00;
                double totalNumber = 00;

		dollarNumber = Double.parseDouble(dollarField.getText());
		


		total = dollarNumber * johnPercent;
		total = dollarNumber * tomPercent;

		totalField.setText("$" + totalNumber);
		damtDisplay.setText("" + total);
	

 	 }

My swing is a little rusty but if you add an Object in a comboBox, the toString() method will be displayed. And when you try to get the selected item it will return the entire object.

So have an object Person with attributes: name, percentage.
The Person should have a public String toString() { return name; } method.
So when you add the Person john with values:
name="John"
percentage=0.15
to the JComboBox it will be display what the toString method returns: "John"
When you do combo.getSelectedItem(); NOT combo.getSelectedIndex(); you will get the object Person:
Person selectedPerson = (Person)nameCombo.getSelectedIndex();
then selectedPerson will not only have the name but the percentage as well. So you will get it:
selectedPerson.getPercentage(); and multiply with whatever you want and set it to another field.
I have never tried the above approach, but I think that it will work. Also I hope I understood correctly you problem.

Comments
Nice help. A true Java Addict :) !

I wasn't able to figure it out. Whenever I did the public String toString() { return name; }
I would get an error message. Maybe I'm doing it wrong but I wouldn't know if I am. From the example I have listed how can I implement it in that code. An example would be the greatest help I can get, even if it doesn't relate to what I posted. I would post the whole code if I could but it's 700 lines long. I have five cases that I'm trying to keep separate. For example I have a JComboBox that has 5 items. Television, Radio, Game, PC and Movie are the five items in my JComboBox. I'm trying to add a percentage to each of them. Like 10% for television, 15% for Radio and so forth. I know how to add the percentage to each item. The problem I'm having is when I multiply the percentage to the cash amount. I want the total to display in the same field. When someone picks television it will show the total for that, or if they pick game, it will show the total for that in the same field. Thanks again for the help and I appreciate you responding.

Where did you add the method toString, and what errors do you get? Perhaps post only the class that you tried to add the toString() method.

If you know how to get the percentage from the comboBox then also get the text from the textfield, multiply them and set the result wherever you want:

double percentage=0.15;
double d = Double.parseDouble( textField1.getText() );
double result = percentage * d;
 textField1.setText( result );

Thanks a lot. Everyone on here has been so helpful. I can't wait until I get to the stage where I'm able to help others.

This article has been dead for over six months. Start a new discussion instead.