• Post Reply Bookmark Topic Watch Topic
  • New Topic

GUI not updating  RSS feed

 
Nate Benton
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am Working on a game where the user is a character who must avoid falling rocks. The game works fine until i try to play it again without restarting it. The problem occurs when the game over screen is displayed and i click on a PlayAgain button which has an action listener. In the actionPerformed() method i attempt to reset the game to play as if i had just started it. Instead the DataProc method runs and does not display anything to the GUI, as this is happening the GUI seems to be frozen on the GameOver screen.

I am a beginner at java, especially java swing, so any tips rather than just a solution to my problem would be appreciated.
Sorry for the large amount of code posted I'm not sure what is needed to answer my question.

heres the code:
DataProc:


GUI:


thanks,
 
Campbell Ritchie
Marshal
Posts: 55772
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

That is too much code for us to read and go through, though I do notice you are using == true or == false Never use them. Not if (b == true)… or if (b == true)… but if (b)… amd if (!b)… Using == is not only poror style but also error‑prone; you might write = by mistake.
What is the state of your game at the beginning and how are you restoring that state? What are you calling from the listener on the replay button? Do you enable or disable that button ever?
 
Nate Benton
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From The actionPerformed() method i have code to remove the GameOver Jpanel and then to load the ActionPanel back onto the Main Frame.

I found that calling the DataProc() method stops the GUI from updating and i am not sure why. if i do not call DataProc() the GUI updates but the rest of the program does not run.

I do enable the PlayAgain Button I just never disable it.

This is from DataProc, it is the actionPerformed():
 
Campbell Ritchie
Marshal
Posts: 55772
163
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you adding and removing panels? Don't do that. Set up the display once and leave it at that. You can make components appear to the front and disappear to the back with Card Layout.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your problem is that when you run DataProc from the actionPerformed method it is running in the GUI thread. It a long running task that contains a while(true) statement. This will be consuming 100% of the GUI thread's time so it won't have time to update the use interface.

You need to move DataProc to a background thread. Look up SwingWorker for a class which should help you fix the problem. Also read the swing section of Oracle's java tutorial. I believe it has a section on using threads with swing.
 
Nate Benton
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried using card layout, but the problem persisted. Eventually the Screen did change it just waited until DataProc() was done running. So i then tried multithreading, it fixed the problem of it not Changing screens, but now my keyListeners will not work. I think this is because i am using "this" in my addKeyListener() method.




this is where i start the new thread. this code resides in DataProc class.


I start doInBackground in my actionPerformed() method.


the KeyListener() methods are located in DataProc class
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!