Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

addNotify not called

 
Darren Brooks
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Darren Brooks
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic