Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Start Button Freezing

 
John C Stewart
Greenhorn
Posts: 15
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am making a simple button clicker with an interface used to start, stop and set delay between clicks. Everything goes fine with the delay set and the start but once start is pressed it hangs making it impossible to click the stop button and halt the loop. I can not see what the problem is, could someone please look at the code posted below and give me a hint?

 
Steve Luke
Bartender
Pie
Posts: 4181
21
IntelliJ IDE Java Python
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are putting all your work in the Event Dispatch Thread, which is the same thread used for user interaction. Since the Event Dispatch Thread is thoroughly blocked by your loop you can no longer press buttons and such. If you want to still be able to interact with the GUI while your loop does its work you need to move that work into a different thread. One way to do that rather painlessly is to use SwingWorker.
 
Paul Clapham
Sheriff
Pie
Posts: 20188
26
MySQL Database
  • 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And for more detail you could read the tutorial: Lesson: Concurrency in Swing.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47281
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think this thread would fit better into our GUIs forum, so shall move it.
 
John C Stewart
Greenhorn
Posts: 15
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I cannot make this work, I do not understand how to use the SwingWorker. I have read the tutorials but my program is not returning anything so how would the doInBackground method work for me? This is an infinite loop that clicks and releases the left mouse button and that is it, nothing is calculated nothing is returned. Can someone explain this to me with a relevant example?
 
Steve Luke
Bartender
Pie
Posts: 4181
21
IntelliJ IDE Java Python
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, you don't have to use the SwingWorker - it is just a tool to move code to a background thread. If you don't need any interaction with the GUI, then you don't really need it. So just use a regular Thread:

 
John C Stewart
Greenhorn
Posts: 15
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got it using SwingWorker after all. All you have to do is leave the Generic off of the SwingWorker and return null on the doInBackground method to get it to work. See below if anyone ever ends up where I was.

 
Darryl Burke
Bartender
Posts: 5115
11
Java Netbeans IDE Opera
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All you have to do is leave the Generic off of the SwingWorker and return null

What's wrong with <Void, ...> and a return type of Void (you would still return null)?
 
Rob Spoor
Sheriff
Pie
Posts: 20381
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Void is often used because it cannot be instantiated, and therefore has only one valid return value - null. Object could be another choice.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic