User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 397,684 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,489 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: 475 | Replies: 4
Reply
Join Date: Aug 2007
Posts: 57
Reputation: NycNessyness is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
NycNessyness NycNessyness is offline Offline
Junior Poster in Training

JComboBox

  #1  
May 16th, 2008
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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Dec 2007
Location: Greece
Posts: 459
Reputation: javaAddict is on a distinguished road 
Rep Power: 1
Solved Threads: 48
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Pro in Training

Re: JComboBox

  #2  
May 16th, 2008
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.
I AM the 12th CYLON
Reply With Quote  
Join Date: Aug 2007
Posts: 57
Reputation: NycNessyness is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
NycNessyness NycNessyness is offline Offline
Junior Poster in Training

Re: JComboBox

  #3  
May 16th, 2008
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.
Reply With Quote  
Join Date: Dec 2007
Location: Greece
Posts: 459
Reputation: javaAddict is on a distinguished road 
Rep Power: 1
Solved Threads: 48
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Pro in Training

Re: JComboBox

  #4  
May 17th, 2008
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 ); 
I AM the 12th CYLON
Reply With Quote  
Join Date: Aug 2007
Posts: 57
Reputation: NycNessyness is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
NycNessyness NycNessyness is offline Offline
Junior Poster in Training

Re: JComboBox

  #5  
May 17th, 2008
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.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb Java Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the Java Forum

All times are GMT -4. The time now is 12:48 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC