Re: Callbacks, and using interfaces as arguments



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/
.



Relevant Pages

  • Re: Roguelike Interface
    ... I've made a screenshot of my roguelike for you, ... The interface is MDI. ... a set of child windows which are contained inside one parent window. ... The player can make any layout they like, ...
    (rec.games.roguelike.development)
  • Re: the new interface
    ... multi-documents-based applications in MacOS X (like QuickTime, AppleWorks, TextEdit, etc. which keep the multi-window interface). ... As such, it seems appropriate to me that all associated components should be contained in a single window, while each window represents a unique RB document. ... TextEdit, ... The important thing to realize is the difference between separating documents into different windows, and separating information into different windows. ...
    (comp.lang.basic.realbasic)
  • Re: List boxes in Microfocus Dialog System/Net Express?
    ... > are the row indexes of the drop-down and that's the value that's ... > The interface works quite well for choosing the status from the list ... > yet another window to the process (this window is for a small sub-set ... I fired up Word and, using the default settings, Times Roman 11.5pt, I typed ...
    (comp.lang.cobol)
  • Re: UI subsystem interface design
    ... The application constructs its UI by composing the primitive drawing objects and then giving those objects as data to the subsystem, which will be responsible for doing the drawing based upon the data it is given. ... Each window is a rectangle and contains coordinates for its position represented as floating-point numbers between 0 and 1. ... There has to be a mapping of the message data packets in the subsystem interface and you will need some way to ensure that mapping, but I don't see that being dedicated identity objects. ...
    (comp.object)
  • Adding a GUI to a Forth system
    ... recently been experimenting with developing an user interface for my ... taken is for Forth to send a single byte command out over stderr, ... byte command for text display is sent out on stderr, ... stdout to be received by VB and displayed in a window. ...
    (comp.lang.forth)