File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problem with actionListeners

 
Gabriel White
Ranch Hand
Posts: 233
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Campbell Ritchie
Sheriff
Pie
Posts: 47293
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)

private JRadioButtonMenuItem setThicknessButton(String name, ButtonGroup
buttonGroup)
{
thicknessMenuItem = new JRadioButtonMenuItem(name, false);
thicknessMenuItem.setMnemonic(name.charAt(0));
/*{
public void actionPerformed(ActionEvent e)
{
canvas.setThickness(Integer.parseInt(e.getActionCommand()));
}
});*/
buttonGroup.add(thicknessMenuItem);
return thicknessMenuItem;
}
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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic