Results 1 to 2 of 2
  1. #1
    icedust is offline Member
    Join Date
    Oct 2011
    Posts
    2
    Rep Power
    0

    Default super.addNotify()

    I'm reading Killer Game Programming in Java, however I'm confused about something, and that is how addNotify works exactly.

    In an effort to understand, I did this:
    Java Code:
    public class GamePanel extends JPanel implements Runnable
    {
    
    	// some variables .......
    	public GamePanel()
    	{
    		System.out.println("Constructor started");
    		setBackground(Color.white);
    		setPreferredSize(new Dimension(PWIDTH, PHEIGHT));
    
    		// create game components
    		// ..
    		System.out.println("Constructor ended");
    	} // END OF GAMEPANEL
    
    	@Override
    	public void addNotify()
    	{
    		System.out.println("addNotify started");
    		super.addNotify();
    		startGame();
    		System.out.println("addNotify ended");
    	}
    // some more stuff, not relevant
    }
    The JFrame:
    Java Code:
    public class Main extends JFrame
    {
    	
    	public Main()
    	{
    		System.out.println("Constructor main started");
    		setTitle("game");
    		setSize(500, 400);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		add(new GamePanel());
    		try
    		{
    			Thread.sleep(3000);
    			System.out.println("setVisible getting called");
    		} catch (InterruptedException e)
    		{
    			e.printStackTrace();
    		}
    		setVisible(true);
    		try
    		{
    			System.out.println("setVisible ended");
    			Thread.sleep(3000);
    		} catch (InterruptedException e)
    		{
    			e.printStackTrace();
    		}
    		System.out.println("Constructor main ended");	
    	}
    	
    	public static void main(String[] args) {
    		new Main();
    	}
    
    }
    The result was this:

    Constructor main started
    Constructor started
    Constructor ended
    (*Imagine a 3 second delay here*)
    setVisible getting called
    addNotify started
    addNotify ended
    setVisible ended
    (*Imagine another 3 second delay*)
    Constructor main ended


    So it is clear addNotify gets called only when setVisible() of the JFrame is called, and that is desired I suppose. But why? GamePanel isn't an subclass of Main, is it? Maybe I'm just tired, but I dont get how this works exactly
    Last edited by icedust; 03-15-2012 at 07:13 PM.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,013
    Rep Power
    10

    Default Re: super.addNotify()

    GamePanel is a subclass of JPanel. The JPanel is added to the JFrame, so the JPanel becomes visible when the JFrame becomes visible.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. super fun dog
    By uc200511 in forum Entertainment
    Replies: 1
    Last Post: 07-29-2012, 12:56 AM
  2. super
    By diamonddragon in forum New To Java
    Replies: 3
    Last Post: 01-29-2012, 11:49 PM
  3. calling variable using super super..
    By Stephen Douglas in forum New To Java
    Replies: 7
    Last Post: 08-16-2010, 07:12 AM
  4. super
    By dj kourampies in forum New To Java
    Replies: 5
    Last Post: 01-23-2009, 04:07 PM
  5. this() vs super()
    By Hevonen in forum New To Java
    Replies: 9
    Last Post: 12-04-2008, 02:43 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •