Win a copy of Succeeding with AI this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Junilu Lacar
Sheriffs:
  • Tim Cooke
  • Jeanne Boyarsky
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • fred rosenberger
  • Frits Walraven

PaintComponent method in added JPanel is not being called by repaint. Threading problem?

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
Thanks.
 
Master Rancher
Posts: 3728
34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

check if there's something obviously wrong


How can the code be compiled and executed for testing?  It doesn't appear to be complete.  

Can you make a small, complete program that compiles and executes for testing?
 
Marshal
Posts: 25463
65
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Paul Clapham
Marshal
Posts: 25463
65
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yuck. I was wondering how this code even compiled:



And then I realized: display is a Runnable object! Yuck. It's a JFrame, and it's a Runnable too? That really makes your code incomprehensible.

Okay. Making something implement Runnable just so you can run one of its methods is a horrible idea. It's much easier to understand and to write if you just use a lambda there:



Then you don't have to call the method run either, you can give it a meaningful name.

So no, I was wrong to say that you were blocking the event queue. But you still ought to just use a Swing Timer. Then you don't need any of that SwingWorker machinery at all.
 
Rancher
Posts: 4551
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you forcing a repaint of the JFrame?

Indeed, I don't see why you are extending JFrame in the first place.

It looks like you just need the panel to be redrawn each time.
 
bacon. tiny ad:
Two software engineers solve most of the world's problems in one K&R sized book
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic