Don't forget to use the Code Formatting Tags so the posted code retains its original formatting. That is done by selecting the code and then clicking on the "Code" button above the question input area.
Joined: Oct 31, 2008
Rob Camick wrote:
But anonymous inner classes and reusable don't really go well together, do they?
I guess you don't understand the concept of promoting better coding techniques. I try to give helpfull tips.
Actually, at that point, you didn't. You just dissed his comment as useless and did not, in fact, give helpfull tips (well before, that was very helpful, but not in that comment). I am thankful for the advice from both of you, as you are obviously more experienced than I, but do keep it civil, please. Other coders, other solutions.
As to my problem, I did use the code tags, right? But what was actually missing was more casting - I have to cast .set as String and .dayOfYear as int, now it's working fine.
Joined: Jun 13, 2009
You just dissed his comment as useless
I never said it wouldn't work.
and did not, in fact, give helpfull tips
My tip was "it doesn't promote reusable code".
Yes, there is always more than one way to solve a problem but too many people get in the habbit of doing shortcuts and they don't know the reason why.
My comment was about how to write code for an ActionListener (or any listener in general) and had nothing to do with inner classes.
Maybe in this simple case the inner class is not reusable but if you ever do decide to refactor and create a separate class the job is much more straight forward. Also if you get in the habbit of using this approach then when you do need a separate class you don't need to ask another question about how to do it.
If the question is specifically restricted to a button changing its own text, then Rob might be right and it's better in the long run to do the cast inside the ActionListener.
However that fails when the button wants to change the text of some other button. So, not all that reusable after all in this case.
Point being, you have to look at the requirements. Naturally we don't have all the requirements because we're just being told about a specific coding problem. And sometimes the requirements can change and make a mess of the solution you thought was reusable or extensible or whatever. That's just life in the programming biz.
Joined: Oct 31, 2008
I'm not even in any biz. Well, the medical biz, I guess. I just java for fun, and creating shiftplans for on-call doctors with java will save time in the years to come. But I don't have enough projects to really have need for reusable coding.
I do see the difference in the two approaches though and am very grateful for both. In the end you guys took the time to help out, little noob that I am, so thank y'all very much.
Just got done with the project. Will not post the code to keep anyone from suffocating with laughter