Results 1 to 11 of 11
  1. #1
    StainedWolf is offline Member
    Join Date
    Aug 2014
    Posts
    5
    Rep Power
    0

    Default JFrame dispose() works only first time

    Hey

    The german java forum is currently down so I'll post my problem here ;)

    I'm working on a little framework for future 2D Games and Level Editors. I wanted to implement the ability to switch between fullscreen, windowed and windowed fullscreen mode (yeah i realized that java's fulscreen mode is nothing else than a windowed fullscreen mode x3 ). with the GameFactory class I create a GameFrame in fullscreen mode, let it run 5 seconds and then it shall switch (destroy old window via dispose and create a new GameFrame) to windowed mode. That's all working how I want it to. But the next time i want to switch from windowed mode to windowed fullscreen mode and destroy the windowed fullscreen frame the dispose method isn't working anymore. Please help. There's no Exception that dispose could throw and i don't know a GetLastError() function, so it's difficult to find the problem :(

    Java Code:
    import javax.swing.JFrame;
    
    public class GameFrame extends JFrame
    {	
    	public enum WindowMode
    	{
    		WINDOWED,
    		FULLSCREEN,
    		WINDOWED_FULLSCREEN
    	}
    	
    	WindowMode windowMode;
    	
    	public GameFrame()
    	{
    		super();
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	}
    	
    	public static void main(String args[])
    	{
    		GameFactory factory = new GameFactory();
    		GameFrame window = factory.createGameFrame(WindowMode.FULLSCREEN);
    		sleep(5000);
    		factory.changeWindowMode(WindowMode.WINDOWED, window);
    		sleep(5000);
    		factory.changeWindowMode(WindowMode.WINDOWED_FULLSCREEN, window);
    		sleep(5000);
    		factory.retireScreen(window);
    	}
    	
    	static void sleep(long msec)
    	{
    		try
    		{
    			Thread.sleep(msec);
    		}
    		catch(Exception e)
    		{
    			e.printStackTrace();
    		}
    	}
    }
    Java Code:
    import java.awt.GraphicsDevice;
    import java.awt.GraphicsEnvironment;
    
    public class GameFactory 
    {
    	GraphicsDevice device;
    	
    	public GameFactory()
    	{
    		device = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
    	}
    	
    	public GameFrame createGameFrame(GameFrame.WindowMode mode)
    	{
    		GameFrame window = new GameFrame();
    		setWindowMode(mode, window);
    		return window;
    	}
    	
    	public void setWindowMode(GameFrame.WindowMode mode, GameFrame window)
    	{
    		switch(mode)
    		{
    		case WINDOWED_FULLSCREEN:
    		case FULLSCREEN:
    			window.setUndecorated(true);
    			window.setResizable(false);
    			break;
    		case WINDOWED:
    			window.setUndecorated(false);
    			window.setResizable(true);
    			break;
    		}
    		window.windowMode = mode;
    		window.setSize(device.getDisplayMode().getWidth(), device.getDisplayMode().getHeight());
    		window.setLocation(0, 0);
    		window.setVisible(true);
    	}
    	
    	public void retireScreen(GameFrame window)
    	{
    		//window.setEnabled(false);
    		//window.setVisible(false);
    		window.dispose();
    		window = null;
    		if(device.getFullScreenWindow() != null) device.setFullScreenWindow(null);
    	}
    	
    	public void changeWindowMode(GameFrame.WindowMode mode, GameFrame window)
    	{
    		if(mode != window.windowMode)
    		{
    			retireScreen(window);
    			window = new GameFrame();
    			setWindowMode(mode, window);
    		}
    	} 
    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: JFrame dispose() works only first time

    I'm not certain what you are doing exactly. But it appears you are creating a new Window in your changeWindowMode() method.
    But you never save an instance of the window for later use.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    StainedWolf is offline Member
    Join Date
    Aug 2014
    Posts
    5
    Rep Power
    0

    Default Re: JFrame dispose() works only first time

    Well I don't know if java is as smart as I think.
    I call changeWindowMode() form the main function and hand it the window object I created in the main function. Then the changeWindowMode() method creates a new GameFrame and assigns it to the window object which origin is in the main function. Isn't the window object variable just a pointer which obtains a new pointee to a new GameFrame in the changeWindowMode() method?

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: JFrame dispose() works only first time

    Nope! That won't work. Java passes arguments by value. So changing the value of a passed argument (window in your case) does not
    change the original passed variable in the calling program. So if you pass in an int of 10 and change it to 20, the original int when you
    return will be 10. Same thing for object references. You can change the state of the object that the reference refers to but
    not the reference itself.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    StainedWolf is offline Member
    Join Date
    Aug 2014
    Posts
    5
    Rep Power
    0

    Default Re: JFrame dispose() works only first time

    Hm ok :/ So I have to find a workaround :(

    Thank you very much!

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: JFrame dispose() works only first time

    Why not just save the passed or created window in the factory. Then have a get method that returns that instance. Or simply return the
    instance via the factory method calls.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    StainedWolf is offline Member
    Join Date
    Aug 2014
    Posts
    5
    Rep Power
    0

    Default Re: JFrame dispose() works only first time

    Yep. Was my solution.
    Here's the edited code parts:

    Java Code:
    public static void main(String args[])
    	{
    		GameFactory factory = new GameFactory();
    		GameFrame window = factory.createGameFrame(WindowMode.FULLSCREEN);
    		sleep(5000);
    		window = factory.changeWindowMode(WindowMode.WINDOWED, window);
    		sleep(5000);
    		window = factory.changeWindowMode(WindowMode.WINDOWED_FULLSCREEN, window);
    		sleep(5000);
    		factory.retireScreen(window);
    	}
    Java Code:
    public GameFrame changeWindowMode(GameFrame.WindowMode mode, GameFrame window)
    	{
    		if(mode != window.windowMode)
    		{
    			retireScreen(window);
    			GameFrame temp = new GameFrame();
    			setWindowMode(mode, temp);
    			return temp;
    		}
    		return window;
    	}
    It's not bad but kinda more complicated... at least for me >.<
    Still need to pass it the window object, so that i can extract and add the child components to the new window.
    But I don't have a concept for that, yet. ;3

  8. #8
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: JFrame dispose() works only first time

    No, that's not what Jim was suggesting. Jim was suggesting this:

    Java Code:
    public class GameFactory
    {
        private GraphicsDevice device;
    
        // CHANGE: window is property of factory class
        private GameFrame window;
         
        public GameFactory()
        {
            device = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
        }
         
        public GameFrame createGameFrame(GameFrame.WindowMode mode)
        {
            // CHANGE: set in object property
            window = new GameFrame();
            setWindowMode(mode, window);
            return window;
        }
    
        // CHANGE: method doesn't need to be fed back the GameFrame instance anymore. If you look at it now, isn't this method almost identical to createGameFrame?
        public void changeWindowMode(GameFrame.WindowMode mode)
        {
            if(mode != window.windowMode)
            {
                retireScreen(window);
                window = new GameFrame();
                setWindowMode(mode, window);
            }
        }
    
    
        // CHANGE: add a method to get the current window instance
        public GameFrame getOpenedWindow(){
          return window;
        }
         
        // CHANGE: method doesn't need to be fed back the GameFrame instance anymore
        public void setWindowMode(GameFrame.WindowMode mode)
        {
            switch(mode)
            {
            case WINDOWED_FULLSCREEN:
            case FULLSCREEN:
                window.setUndecorated(true);
                window.setResizable(false);
                break;
            case WINDOWED:
                window.setUndecorated(false);
                window.setResizable(true);
                break;
            }
            window.windowMode = mode;
            window.setSize(device.getDisplayMode().getWidth(), device.getDisplayMode().getHeight());
            window.setLocation(0, 0);
            window.setVisible(true);
        }
         
        // CHANGE: method doesn't need to be fed back the window anymore
        public void retireScreen()
        {
            //window.setEnabled(false);
            //window.setVisible(false);
            window.dispose();
            window = null;
            if(device.getFullScreenWindow() != null) device.setFullScreenWindow(null);
        }
         
    }

    PS: Java is neither smart nor dumb; it has no brain. You have to do ALL the thinking.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: JFrame dispose() works only first time

    Quote Originally Posted by gimbal2 View Post
    You have to do ALL the thinking.
    That's why my brain's always hurting...:(
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  10. #10
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: JFrame dispose() works only first time

    And then you have the composure and calm to not regularly bang your head on the desk like I do!
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  11. #11
    StainedWolf is offline Member
    Join Date
    Aug 2014
    Posts
    5
    Rep Power
    0

    Default Re: JFrame dispose() works only first time

    Quote Originally Posted by gimbal2 View Post
    PS: Java is neither smart nor dumb; it has no brain. You have to do ALL the thinking.
    I think it's more like: You have to cope with what THEIR thinking has created.
    And with "THEIR" i refer to the java inventors :P And i think u can call java smart (or dump).
    At least it's smarter than C cause it knows how to handle pointers itself ^^
    But it's just my opinion x3

    Quote Originally Posted by gimbal2 View Post
    No, that's not what Jim was suggesting. Jim was suggesting this:
    Thanks. I've reworked my program so that everything exists in the GameFactory class. Less objects to handle in order to create a working window and so on ^^

Similar Threads

  1. JFrame not responding to dispose() function
    By amitSingh in forum New To Java
    Replies: 15
    Last Post: 06-14-2014, 07:17 AM
  2. Intersects() works just first time
    By Szinek in forum New To Java
    Replies: 2
    Last Post: 07-12-2012, 10:44 AM
  3. Java GlassPane Works 30% Of The Time.
    By Alerhau in forum AWT / Swing
    Replies: 15
    Last Post: 08-10-2011, 05:41 PM
  4. Dispose Jframe from another class.
    By vahshir in forum AWT / Swing
    Replies: 1
    Last Post: 05-26-2011, 12:33 PM
  5. i cant dispose the jframe(urgent)
    By arunkumarinfo in forum NetBeans
    Replies: 2
    Last Post: 02-24-2009, 09:19 AM

Posting Permissions

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