• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Start Button Freezing

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?

 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And for more detail you could read the tutorial: Lesson: Concurrency in Swing.
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)?
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic