Sign in to follow this  
stein102

JOptionPane inside of switch statement?

Recommended Posts

I have an JOptionPane inside of my switch statement, but if you take more than a few seconds to enter your input, the program stops responding. Why does this happen? Is there a workaround?

 

				switch(selection){
				case 60:
					brickSelection = 1;
					break;
				case 61:
					brickSelection = 2;
					break;
				case 62:
					brickSelection = 3;
					break;
				case 63:
					fileName = JOptionPane.showInputDialog(null, "What would you like to call
                                        your level?","Save Level",3);
					break;
				case 64:
					sbg.enterState(GameStart.MENU);
					break;
				}

 

Share this post


Link to post
Share on other sites

I think I figured out where the error is. The game is using Slick2D API and the JOptionPane stops the game loop and causes it to stop responding. Is this correct?

Yes, JOptionPane is a modal window, which will stop the code where you open the dialog. If you like to continue your game, you should consider using either a non-modal window or an other thread.

Share this post


Link to post
Share on other sites

I think I figured out where the error is. The game is using Slick2D API and the JOptionPane stops the game loop and causes it to stop responding. Is this correct?

Yes, JOptionPane is a modal window, which will stop the code where you open the dialog. If you like to continue your game, you should consider using either a non-modal window or an other thread.

 

Would there be a way to not have it in another thread? It's just for a level editor and I don't need to have any logic updates while the user is in the JOptionPane.

Share this post


Link to post
Share on other sites

It's just for a level editor and I don't need to have any logic updates while the user is in the JOptionPane.

Ok, it should be clear why the program stops processing while the dialog is open, but the question is, once you close the option pane, does the program continue or is it still not responding ?

 

When you use the time as input parameter for your game, a larger break might break your code. E.g. if you expect an average frame duration of 20-100ms and suddenly you got several seconds, your program could run into trouble.

Share this post


Link to post
Share on other sites

It's just for a level editor and I don't need to have any logic updates while the user is in the JOptionPane.

Ok, it should be clear why the program stops processing while the dialog is open, but the question is, once you close the option pane, does the program continue or is it still not responding ?

 

When you use the time as input parameter for your game, a larger break might break your code. E.g. if you expect an average frame duration of 20-100ms and suddenly you got several seconds, your program could run into trouble.

The program continues after closing the JOptionPane.

 

If you enter the input fast enough, the game doesn't even go unresponsive at all, it's only when you take a few seconds to get the input.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this