• Post Reply Bookmark Topic Watch Topic
  • New Topic

Repaint problems in Swing  RSS feed

 
Jim Venolia
Ranch Hand
Posts: 312
2
Chrome Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want this to do nothing until I press the GO button, then constantly update the loop counter. Starting at line 89 (SwingUtilities.invokeLater) you can see the various ways I've tried to make it work. With everything commented out I get the UI, but it obviously doesn't do anything. Uncomment one of the blocks though, and it just displays the border and title bar. What is the magic incantation to make this work?


I don't understand why this is so difficult. I understand threads, processes, interrupts, and all the other hardware related concepts. In the code above:

1) The GUI is running in my thread. In this case repaint should draw the GUI.
2) The GUI is running in a different thread. In this case Thread.sleep() or Thread.yield() should shut me down and let the GUI thread update the GUI.

If #1 is true the GUI should update when I call repaint. If #2 is true the GUI should schedule the repaint and the sleep should let the GUI do it's thing.

I've read enough to know the GUI is in a different thread. So why doesn't the repaint work when I suspend my current thread via sleep? And why is such a conceptually simple idea (increment a counter as a program runs) so damned difficult to implement?
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read the API for javax.swing.Timer and follow the link to the tutorial on How to Use Timers.

Then find the section of the same tutorial that deals with Concurrency in Swing, where you can find more of the theory behind Swing threads, and other useful code samples.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!