Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

need some critics  RSS feed

 
jrookie
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello
i written my own code, it's working but just want know what think another people about actionPerformed() method
i can build it only that way ? (i don't need inn classes)
public class MMT extends JFrame implements ActionListener
...
m11=new JMenuItem(m11s);
m11.addActionListener(this);
m12=new JMenuItem(m12s);
m12.addActionListener(this);
m13=new JMenuItem(m13s);
m13.addActionListener(this);
...
public void actionPerformed(ActionEvent e)
{
if (e.getSource()==m11)
{
System.out.println("hello m11");
}
if (e.getSource()==m12)
{
System.out.println("hello m12");
}
...
}
 
Brian Duff
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course, that code is fine, but personally I dislike exposing unnecessary public APIs because of implementing listener interfaces at that level. To me, the actionPerformed() method is part of the internal implementation of that class, so I'd prefer not to expose it.
I often use anonymous inner classes and follow this pattern when using ActionListeners:

The above approach hides the implementation from users of MyClass, but gives you some flexibility. You could make doItem1() and doItem2() public if you wanted to provide a way for users of this class to programmatically click the buttons without exposing the buttons themselves. The above example makes them protected, so it's easy for subclasses to change the behavior when the buttons are clicked without dealing with event handling code. You could also make them private.
Another commonly used alternative is to use Actions. Actions are quite a nice way to handle menu items, buttons and toolbar items in a consistent way and are very similar to the Command pattern. See the javadoc for javax.swing.Action for details.
Thanks,
Brian
[ August 23, 2002: Message edited by: Brian Duff ]
 
jrookie
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
inner class are good
but if you need set some parameters or get them from this inner class
how you make it ?
thanks
[ August 23, 2002: Message edited by: jrookie ]
 
Brian Duff
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's just the thing: I tend to avoid putting too much implementation into the inner class itself: you can see in the above implementation that the anonymous inner class is really lightweight. It's sole purpose is to bind two methods in the class to the buttons.
Having said that, if you wanted to, you can easily access information from the enclosing class from inside the inner class (I'm already doing this by accessing the member variables for the button). You can also pass information in via the inner class constructor and with anonymous classes, you can even access variables defined in the same method (provided they are marked final).
I tend to avoid putting too much into inner / anonymous inner classes when I can help it. I've often had to subclass code written by Sun where there is a lot of implementation in an inner class - this makes it very hard to override a small amount of behavior in a subclass.
Brian
 
jrookie
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how i can use constructor in inner anymous class ?
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"jrookie" -

Welcome to the JavaRanch! Please adjust your displayed name to meet the
JavaRanch Naming Policy.
You can change it here.

As for the answer to your question, how do you mean "use constructor"? You can call any constructor of the super class when defining an abstract inner class. However, you cannot override any constructors in the anonymous class because it is anonymous, so you don't have a specific class name to name the constructor...

Thanks! and welcome to the JavaRanch!
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!