Combo box for an if

Want to use a combo box selection in an if statement

Page 1 of 1

4 Replies - 11034 Views - Last Post: 17 January 2009 - 04:26 PM Rate Topic: -----

#1 crummydo  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 223
  • Joined: 06-January 09

Combo box for an if

Posted 16 January 2009 - 05:07 PM

I am building the second installment of my temperature converter. I already completed a celsius to to fahrenheit converter, but not I need to give the option of coverting between both. I thought it would be best to have a text field, then a drop down combo box to choose between the two, and the press the Convert button to display in a label field the calculated value. But how do I use the combo box choices in an if statment?

 private void convertActionPerformed(java.awt.event.ActionEvent evt) {										
		if(typeSelect = Fahrenheit)//if converting Fahrenheit to Celsius
		{
		int tempcels = (int)((Double.parseDouble (degreesTextField.getText()) - 32) * 5.0/9.0 );
		jLabel1.setText(tempcels + " Celsius ");
		}

		if(typeSelect = Celsius) //if converting Celsius to Fahrenheit
		{
			int tempfahr = (int)((Double.parseDouble (degreesTextField.getText())) + 32);
			jLabel1.setText(9.0/5.0 * tempfahr + " Fahrenheit ");
		}
}		



Is This A Good Question/Topic? 0
  • +

Replies To: Combo box for an if

#2 BigAnt  Icon User is offline

  • May Your Swords Stay Sharp
  • member icon

Reputation: 101
  • View blog
  • Posts: 2,392
  • Joined: 16-August 08

Re: Combo box for an if

Posted 16 January 2009 - 05:18 PM

JComboBox has getSelectedItem and getSelectedIndex methods which return the selected item in the combo box and the index of the selected item in the list respectively, once you have the item or index you can use an if to check if that item or index corresponds to Fahrenheit or Celsius
Was This Post Helpful? 1
  • +
  • -

#3 crummydo  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 223
  • Joined: 06-January 09

Re: Combo box for an if

Posted 17 January 2009 - 11:53 AM

I get what you are saying, but how do I do that. I am a beginner so bear with me. Do you know of any good tutorials on this?

Also, I am getting errors in net beans that is saying that a boolean is required. What am I doing wrong?
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,908
  • Joined: 06-March 08

Re: Combo box for an if

Posted 17 January 2009 - 02:28 PM

Required boolean is your if(typeSelect = Celsius) instead of if(tyepSelct == Celsius) With == not =

What BigAnt explained to you is test the Combo in your actionPerformed()

Assuming your combo contains "Farenheight" as first item and "Celsius" as second item

public void actionPerformed(ActionEvent e) {
   if(combo.getSelectedIndex() == 0) {
		... convert from Farenheight to Celsuis
   }
   else	  // getSelectedIndex() has to be 1
   {
		... convert from Celsius to Farenheight
   }
}


Was This Post Helpful? 0
  • +
  • -

#5 crummydo  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 223
  • Joined: 06-January 09

Re: Combo box for an if

Posted 17 January 2009 - 04:26 PM

Hell yeah, that does it. Code worked but my math was off bad. It converted 32f to 0c, but 0c to 57f. Obviously not correct. I fixed it though. Thanks for the help.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1