•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 375,165 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,344 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 381 | Replies: 4
![]() |
•
•
Join Date: Aug 2007
Posts: 56
Reputation:
Rep Power: 1
Solved Threads: 0
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);
} Last edited by NycNessyness : May 16th, 2008 at 4:39 am. Reason: Messed up syntax
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.
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.
I AM the 12th CYLON
•
•
Join Date: Aug 2007
Posts: 56
Reputation:
Rep Power: 1
Solved Threads: 0
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.
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:
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 );
I AM the 12th CYLON
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb Java Marketplace
Similar Threads
- Set / Get jComboBox value and Item. (Java)
- horizontal line/rule in JComboBox? (Java)
- comparing a value to a value in a jComboBox (Java)
- Display Value in JcomboBox (Java)
- JComboBox -- Item is not in the list???????? (Java)
- java uses or overrides a deprecated API?? (Java)
Other Threads in the Java Forum
- Previous Thread: Moving Walls in an Array
- Next Thread: Certification or experience


Linear Mode