if statement problem
Hi I got a problem with my if statement. I am trying to enable some buttons that are disabled by default. as long as there is something in all those text fields. I am trying the following code:
if (event.getSource() == ticketCombo)
{
if (priceField.getText() != "" && originField.getText() != "" && destinationField.getText() != "")
{
twentycButton.setEnabled(true);
fiftycButton.setEnabled(true);
onedButton.setEnabled(true);
twodButton.setEnabled(true);
fivedButton.setEnabled(true);
tendButton.setEnabled(true);
twentydButton.setEnabled(true);
fiftydButton.setEnabled(true);
}
However, no matter if there is nothing in one of the fields listed, it enables all the buttons anyway, it's like it is not in an if statement at all. It just enables all the buttons when I click the combo box.
Anyone know what I am doing wring?
Thanks
[951 byte] By [
Duskana] at [2007-10-3 8:26:00]

you cant compare strings with == and !=
you should be using
if("something".equals("something")) // this is true
== and != compare memory locations
infact, i would advise you to use .equals whenever you compare any objects, and == whenever you compare primatives, it will get you out of alot of trouble in the long run
use equals, not == or != for comparing string.
if ("".equals(str))
if (!"".equals(str))
You can also do str.equals("") of course, but then you have to either first check for str != null, or be certain that str cannot be null.
> if (priceField.getText() != "" && originField.getText() != "" && destinationField.getText() != "")
use instead: -
if( !priceField.getText().equals("") && !originField.getText().equals("") && !destinationField.getText().equals("")
you need to use the equals(String s) method of String to compare strings.
!= won't work for strings.
Now that you've been steered in the right direct as far as String comparisons are concerned, to respond to your original request...
> I am trying to enable some buttons that are disabled by default, as
> long as there is something in all those text fields.
What if the buttons do get enabled, then one of the trigger fields gets blanked out, then the ticketCombo event is fired again?
Don't you want them to revert back to being disabled? You might want something like this:
if (event.getSource() == ticketCombo)
{
boolean enable = !priceField.getText().equals("") &&
!originField.getText().equals("") &&
!destinationField.getText().equals("");
twentycButton.setEnabled(enable);
fiftycButton.setEnabled(enable);
onedButton.setEnabled(enable);
twodButton.setEnabled(enable);
fivedButton.setEnabled(enable);
tendButton.setEnabled(enable);
twentydButton.setEnabled(enable);
fiftydButton.setEnabled(enable);
}
Or, you might want to put a listener on each of those trigger fields to do the enabling/disabling there.