I want to make a GUI application in which I want to do something continuously (i.e. in while loop) in a different thread, until the user presses a button. In this other thread, I am accessing GUI elements and hence I have to use SwingUtilities.invokeLater() for this thread. But since this thread never ends, the Event Dispatching Thread's event handler for the button would never be called. How to handle this situation ?
daudiam
-8
Junior Poster
Recommended Answers
Jump to PostCan you use the SwingUtilities method ONLY for the GUI interface calls and let your code run on its own, non EDT, thread?
Can you make a small simple program that compiles and executes to demo the problem?
Jump to PostYou haven't moved the code out of the EDT. ONLY use the EDT when changing the labels. For the sleep do that in your thread.
Its more efficient to get the images ONE time outside of the loop and then reference them inside the loop when they are needed.
Jump to PostNever - that is NEVER - use sleep anywhere near Swing. Your while(true) loop runs on the EDT and never returns, thus blocking everything.
For that kind of timing use a javax.swing.timer
Use it to call your button changing method every x millisecs. In that method just change the icon …
Jump to PostIf you read the API for the Timer class you will see:
the action handlers for Timers can safely perform operations on Swing components.
James's suggestion for using a Timer is the way to go.
I have gotten your code to work by modifying it in the way I suggested. …
All 13 Replies
NormR1
563
Posting Sage
Team Colleague
daudiam
-8
Junior Poster
NormR1
563
Posting Sage
Team Colleague
JamesCherrill
4,667
Most Valuable Poster
Team Colleague
Featured Poster
daudiam
-8
Junior Poster
NormR1
563
Posting Sage
Team Colleague
JamesCherrill
4,667
Most Valuable Poster
Team Colleague
Featured Poster
daudiam
-8
Junior Poster
NormR1
563
Posting Sage
Team Colleague
JamesCherrill
4,667
Most Valuable Poster
Team Colleague
Featured Poster
daudiam
-8
Junior Poster
JamesCherrill
4,667
Most Valuable Poster
Team Colleague
Featured Poster
daudiam
-8
Junior Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.