One of the things that attracted me to Java was the so called "write once, run anywhere" nature of the JVM, and although I believed it all at the time I"m beginning to wonder if the doubters weren't right.
In some ways this is a good thing though - I was writing a small app for my brother in law on my Mac - which he will run on a PC. I got fed up with Eclipse running at snail pace on the Mac, so switched to the PC, and found quite a difference. One example:
else if (soldOnTF.getText().equals("") { soldOnTF.setText("sdf.format(cal.getTime()) }
The idea was to only input the date into one or other textfields, the decider being whether the first field was already occupied. I know this might not be good Java, but it worked fine on the Mac. On the PC, both textfields are filled with the date - as though the "else if" is being ignored. I've tried loads of ways round this, like finding which field lost focus prior to the button click, or setting a boolean value to true if the first field is populated, but it still seems to fill both (or neither).
I know this sounds like a real dumb suggestion but I wondered if because my PC is so much faster than the Mac, the second statement is executing before the first one has been completed? Yes I know that's stupid, but at 12pm last night it started to make sense ;)
Any help on that bit of code would be very welcome - as would any other things I ought to watch out for when coding on the PC when I"m used to the Mac.
Your boughtOnTF and soldOnTF must be pointing to the same Object hence regardless of the logic statement they will both either be filled or not (nothing to do with execution speed).
What you could do is post the section of code where you declare and instantiate the to textFields, where you have the statements (and make sure they are declared separately) like so:
JTextField boughOnTF = new JTextField(12);
JTextField soldOnTF = new JTextField(12);
and hopefully at some point you did not say
boughOnTF = soldOnTF;
or have a scenario where you equate non-primitive attributes - which depending on where and how you did this it would explain the behaviour.
Both fields are declared separately, and at no time have I equated one to the other. I'm not totally sure what you mean by equating one or other to non primitives, but I'm guessing it sort of amounts to the same thing? This is the only point in the class where I've done anything with the textfields, other than adding them to the gui.
From what you're saying though, it sounds as though you think the code should work? I was wondering if the Mac was being lenient on some duff code, when the PC was being more rigid about it?
Apologies for the mistakes in the code posted - I couldn't find any way to edit it, but the code in the class doesn't have the same errors.
Its almost beer time so, I'll give it a last shot.
> From what you're saying though, it sounds as though you > think the code should work?
In all honesty, I'm a Pos programmer, I work with Swing every day of my life for 9+ hrs a day (on PC/Linux), JTextFields are pretty much stable. So I really think there is definitely something wrong at some point in your code, its definitely doing what you coded it to do. Yes it should evaluate one or the other, but the point is you have an object called sdf or something like that? What exactly is it holding? And to which class is it an attribute of? That's where you could find your crossing the objects to be holding similar values. Can't tell much from that little snippet though.
> Apologies for the mistakes in the code posted -
No worries.
Anyways, good luck with your project, sorry I couldn't be of more help. But I don't want to be scrounging on your hard work and have you give too much detail of your code - lest I be accused of copyright infringement. Enjoy the weekend.
You're probably already in the pub, but I'll reply now while it makes sense to me ;)
The sdf is a Simple Date Format object from the Text class, so I'm just asking it to enter today's date in the textfield when clicked - the idea of only filling the soldOn field if the boughtOn field is already full is that the user may want to come back to some details originally entered some days earlier. Whatever, I don't think that should be causing the problem, but I"ll look through all the rest of it again (and again).
You're welcome to see the full code any time - I seriously doubt you'd want to steal it :D
Thanks again, and a good weekend to you too, Will.