aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Start Button Freezing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Start Button Freezing" Watch "Start Button Freezing" New topic
Author

Start Button Freezing

John C Stewart
Greenhorn

Joined: Oct 24, 2011
Posts: 15
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

Joined: Jan 28, 2003
Posts: 3932
    
  17

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.


Steve
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18109
    
    8

And for more detail you could read the tutorial: Lesson: Concurrency in Swing.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36452
    
  15
I think this thread would fit better into our GUIs forum, so shall move it.
John C Stewart
Greenhorn

Joined: Oct 24, 2011
Posts: 15
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

Joined: Jan 28, 2003
Posts: 3932
    
  17

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

Joined: Oct 24, 2011
Posts: 15
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

Joined: May 03, 2008
Posts: 4522
    
    5

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)?


luck, db
There are no new questions, but there may be new answers.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19538
    
  16

Void is often used because it cannot be instantiated, and therefore has only one valid return value - null. Object could be another choice.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Start Button Freezing
 
Similar Threads
Retrieving tooltiptext from a game....
Problem converting applet to JApplet
Painting in a JPanel?
If statement
Applet and XML Doc through a browser