Java Glossary : addNotify

CMP home Java glossary home Menu no menu Last updated 2004-06-28 by Roedy Green ©1996-2004 Canadian Mind Products

Java definitions: 0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

You are here : home : Java Glossary : A words : addNotify.

addNotify
Makes this Component displayable by connecting it to a native screen resource (peer object). addNotify is called internally by the toolkit and should not be called directly by programs.

This method can be useful when your code requires the native screen peer object to already exist before your code will work, e.g. Component.createImage. You piggyback your code on it like this:

public void addNotify()
   {
   super.addNotify();
   offScreenImage = createImage( width, height );
   }

If you put your Component.createImage code in the constructor, it would sometimes work and sometimes not.

What is going on here? createImage wants to create an image in the native screen format. The only thing that knows which screen and which screen device driver you are using is the peer object in the native GUI. However, the peer does not exist until super.addNotify() returns. If you write an addNotify method that calls super.addNotify, when it returns from super.addNotify, you can rest assured the peer object has been created, and it is now safe to call createImage. So you might as well just tuck you code right after the call to super.addNotify, and it will automatically be called immediately after the peer object has been created.

One disadvantage to putting initialisation code in addNotify, in that you can't mark the instance variable final. Java does not know that addNotify will be called only one.

To get an understanding of how this all works, I suggest peppering your code with debugging messages saying things like constructor X started, constructor X ended, addNotify X started, addNotify X ended etc, to understand the very complex flows of control that can happen, especially when you have overridden methods.

Some rules of thumb: addNotify will not be called until after you constructor completes. It will be called as a side effect of a setVisible( true ); This gives you four places you can put initialisation code that will be executed in this guaranteed order.

  1. in the constructor.
  2. In the caller, just after the new , but before the setVisible( true ) gets called for the overarching JFrame. The caller can use initialisation methods of the newly created Component.
  3. In the Component's addNotify method.
  4. In the caller, after he calls setVisible( true ) for the overarching JFrame. addNotify itself should not call setVisible or you get into an endless loop.

I was under the delusion that addNotify might be called unpredictably at any time the underlying GUI asynchronously felt ready to display. I did not know if there would be a race between it and method calls just after the constructor. It is all happily predictable.

This inialisation technique only works if super.addNotify exists or no one will ever call your addNotify method.

Fortunately, your JPanel addNotify method will be called after the addNotify methods of all the Components in your JPanel.

windowOpened

Another similar sort of place to tuck late initialisation code is in the WindowListener.windowOpened method.

/**
 * Invoked when a window has been opened.
 */
public void windowOpened ( WindowEvent e )
   {
   // Normally no super.windowOpened call is needed.
   // This is pure notification.
   // windowOpened has no duties.
   getMyInfo();
   }

why call it addNotify?

addNotify has nothing whatsoever to do with notifying observers.

Why is it called addNotify instead of createPeer. Perhaps the hook it creates notifies both Java and the native GUI of interesting events.


CMP logo
CMP_home
home
Canadian Mind Products CSS
HTML Checked!
ICRA ratings logo
mindprod.com IP:[24.87.56.253]
Your IP:[80.134.30.163]
You are visitor number 1755.
Please send errors, omissions and suggestions
to improve this page to Roedy Green.
You can get a fresh copy of this page from: or possibly from your local J: drive mirror:
http://mindprod.com/jgloss/addnotify.html J:\mindprod\jgloss\addnotify.html