Re: Callbacks, and using interfaces as arguments
- From: Thomas Hawtin <usenet@xxxxxxxxxxxxxxxxx>
- Date: Sat, 31 Dec 2005 14:50:43 +0000
Mike wrote:
>I have the following code snippets. Both has a method call to addWindowListener
It is my understanding that BookOrderFrame is using a callback method to implement the interface for WindowListener. Am I correct?
LoanCalculatorFrame otoh, does it substantially different and does not even state "implements WindowListener".
WindowListener is a callback interface. A callback is where lower level code calls higher level code. It doesn't matter how the interface is implemented. However, anonymous inner classes are a handy, if verbose, language feature for this kind of work.
Neither LoanCalculatorFrame or BookOrderFrame need extend JFrame as they do not override any methods.
It doesn't make sense for the outer class to publicly implement the interface. So, we can safely say that is wrong.
Java doesn't allow multiple inheritance (of implementation). So as you can see by all the blanks methods in your second example, we lose the ability to make use of abstract implementations.
For simple cases anonymous inner classes are an easy, concise solution. You want to add a window listener that when the window is closing, does something. All nicely together without having to hunt stuff down. The anonymous inner class still gets access to the outer class and final variables of the enclosing method, so you don't need to go into some horrible complication to share common state.
If your problem gets more complex, you can create the anonymous inner class or make it a local inner class. More complex and a member inner class may be more appropriate. Further complexity might result in a static nested class, then on to a full outer class.
Closing the application (or disposing of the window) are such common operations for trivial programs, that JFrame has a convenient method to handle the situation.
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Toolkit tk = Toolkit.getDefaultToolkit(); Dimension d = tk.getScreenSize();
If you use frame.getGraphicsConfiguration, you can cope with multiple screens.
Tom Hawtin -- Unemployed English Java programmer http://jroller.com/page/tackline/ .
- References:
- Callbacks, and using interfaces as arguments
- From: Mike
- Callbacks, and using interfaces as arguments
- Prev by Date: Re: Callbacks, and using interfaces as arguments
- Next by Date: Re: Advantages of using private static methods in a class
- Previous by thread: Re: Callbacks, and using interfaces as arguments
- Next by thread: Advantages of using private static methods in a class
- Index(es):
Relevant Pages
|