Hey there. I've visited coderranch in past but today I joined.
I'm making a small graphics application for displaying a changing colour grid (hopefully with some patterns). After I made a user GUI form to input the run argument values there has been problems. Right now when user sets the probability values and clicks start, the grid display frame pops up, but it is not painted. Sometimes its blank grey. Other times its black. The top right close buttons do not respond.
Possibly there may be a threading problem. I've have made a SwingWorker object, and have added the application-cycle-function into its doInBackground function. Before the cycle function goes into loop it displays the grid Frame object, and passes this object to SwingUtilities.invokeLater function. The terminal output after start button is clicked shows that the main-loop happens, and the run function of the display object also is triggered (this has another loop that calls this.repaint() and sleeps for some time).
The display frame is a JFrame, and has an inner class that extend JPanel (which has the paintComponent funciton. I've added a test terminal print statement to the paintComponent function and it seems the function is not being called. I believe this might be the problem but am not sure why the close buttons do not respond.
Following is part of the code. Sorry there is more of code in the whole program so I've selected just a few parts :
Control Class start frame function: (right at the bottom the mainloop is called within the SwingWorker object)
Control Class Main Loop function : This does the calculations and uses the setBoxColor function of the (JFrame) display object :
This is the whole display Frame class: points to note may be the run method and the inner class DrawPanel
I will continue to hunt for the solution tomorrow. Could you please check if there's something obviously wrong I'm doing here?
Yeah, it's a threading problem. Your run-forever loop is running in the event thread and blocking it from ever doing anything. Fortunately it just wants to repaint every 500 milliseconds, and there's a Timer class in Swing for exactly this kind of thing.
Edit: Also you don't ever call that run() method, not that I can see, so that's why you don't see it doing anything. But if you had, it wouldn't have worked right anyway.