This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
I am trying to create a single method that holds all of the event listeners of my simple drawing class and when I try to incorporate the drawing thickness radio button listeners into that I have problems.
Below are a few snippets of code to show you what I am trying to do, and possibly what I am doing incorrectly. What I end up with is the last radio button to be selected is the one that is used (for drawing). So if I select 1 then I draw with 1, if I select 2 then it draws with a 2, if I select 4 then it draws with a 4. But when I try to go back to 2 or 1 it draws with a 4(the last one selected). I assumed that it was because this was a listener for mulitple actions and that I am adding them in at the same time and that I am creating a new JRadioButtonMenuItem everytime I add something (Basically I have N action listeners, one for each item I create. I have tried to pass this in as a parameter only to get the same results. Can I do this or is there really not an elegant design option here? Ultimately I want to pull all of my listeners into a seperate class (Not Inner), but currently I am testing it in a method.
Thanks in advance. [ April 30, 2007: Message edited by: Gabriel White ]
Don't like the notion of a single class for all your Listeners. I seem to write about it frequently, most recently here in reply to a query by Natasha Henry. I usually have a rule of thumb that you only use an anonymous inner class when you have one and only one Listener doing a particular task. In your case, however, because you have a button-creating method, you could make an exception to that rule. Go back to your original plan
//setThicknessButton() method (stuff moved to setupListeners commented out)
All you have to do is to add the method you commented out as part of an anonymous inner class. Like this:-Notice I have changed the parameter to int width. It is easier to write "" + width than the parseInt() method. And the bit about '0' + width will work nicely as long as width < 10. Even though you are now supposed to pass an int to the setMnemonic method.