File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Threads and Synchronization and the fly likes Threads within a ActionListener? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Threads within a ActionListener?" Watch "Threads within a ActionListener?" New topic

Threads within a ActionListener?

Dennis Putnam
Ranch Hand

Joined: Feb 03, 2012
Posts: 230
I have a JLabel that I want to switch from visible to hidden. That much is working fine. Once it is turned visible, I want to hide it after n seconds. It is turned visible within a JButton ActionListener (if that really matters). Within that listener I tried simply creating a timer with a delay but that apparently blocks until the timer expires. At that point I decided to put the timer into a new thread so it would not block. That seems to work except it gets triggered before the delay expires. Since this is a child thread of the listener, does it immediately trigger because the parent (listener) has ended? How do I accomplish this?
Steve Luke

Joined: Jan 28, 2003
Posts: 4181

You should be using a javax.swing.Timer - these will have a timing thread and execute the action listener you pass to it on the GUI thread. See this tutorial for how to use it.

Dennis Putnam
Ranch Hand

Joined: Feb 03, 2012
Posts: 230
Thanks for the reply. That never came up in my search. I'll try it out.
I agree. Here's the link:
subject: Threads within a ActionListener?
It's not a secret anymore!