aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes addNotify not called Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "addNotify not called" Watch "addNotify not called" New topic
Author

addNotify not called

Darren Brooks
Ranch Hand

Joined: Dec 29, 2005
Posts: 31
Regarding JFrames and JPanels, and addNotify:

Below is my code for a class that manages the current JPanel and adds it to the current JFrame. My question is about addNotify on the JPanel. Why is it that addNotify is not called?

public class GamePanelController {

private GameFrame currentGameFrame;
private GamePanel currentGamePanel;

public void setCurrentGameFrame(GameFrame frame) {
this.currentGameFrame = frame;
}

public void setCurrentGamePanel(GamePanel panel) {
this.currentGamePanel = panel;
}

public void displayCurrentGUI() {
currentGameFrame.getContentPane().setBackground(Color.black);
currentGameFrame.getContentPane().add(BorderLayout.CENTER, (Component)
currentGamePanel);
currentGameFrame.displayFrame();
}

}// close GamePanelController class
Darren Brooks
Ranch Hand

Joined: Dec 29, 2005
Posts: 31
It may be important for me to mention that GamePanel is an abstract class which extends JPanel. The panel objects are subclasses of GamePanel which do not extend JPanel, but which rely upon their super class. I am not sure if this has something to do with the addNotify not being called.... Is it not true that the following would call the correct method?

public void addNotify() {

super.addNotify();
// do other stuff, like image.creatImage(width, height)...
}// close addNotify()
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

addNotify() is used internally by the AWT to connect native resources to Component instances. It's used only for "heavyweight" components -- those that actually are associated with a native resource (i.e., a widget from the platform window system.) All the components in java.awt are heavyweight; most of those in javax.swing are not, including JPanel. A JPanel doesn't have a native "peer" (as they're called) and so addNotify() will never be called for a JPanel.

Why are you overriding addNotify(), anyway? It's not something you should ever have to touch, in general, unless you were doing something really unusual.


[Jess in Action][AskingGoodQuestions]
Darren Brooks
Ranch Hand

Joined: Dec 29, 2005
Posts: 31
Thanks for your reply. In my applications so far, I've used addNotify to let me know when the JPanel was added to the Jframe, so as to avoid certain issues, like setting sizes on the JPanel when it's not there yet, etc. And my current issue of having my Graphics being null, as in image.createImage(w, h).

It's actually worked before, so i'm a bit confused about it not working now. I'm trying to create instances of a JFrame and a JPanel separately (as we normally would...), but then the only difference is that Instead of adding the JPanel to the contentPane inside the JFrame code, I do it from a helper class.

???
Darren Brooks
Ranch Hand

Joined: Dec 29, 2005
Posts: 31
Note: When I add this code to the JFrame:

this.getContentPane().add(BorderLayout.CENTER, new GamePanel());

instead of invoking it from a helper class, addNotify is called in the GamePanel class, and the createImage method works. I not sure what's going on behind the scenes for addNotify to be called only when a component is added inside the parent code...?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: addNotify not called