This week's book giveaway is in the Big Data forum.
We're giving away four copies of Elasticsearch in Action and have Radu Gheorghe & Matthew Lee Hinman on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes using threads or there's another way? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Elasticsearch in Action this week in the Big Data forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "using threads or there Watch "using threads or there New topic

using threads or there's another way?

miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
in a swing standalone app i want a message to appear for a few seconds confirming user did deleted something
i'm using this code:

is there a better/smoother way of doing this?


java amateur
Henry Wong

Joined: Sep 28, 2004
Posts: 19344

Actually, once your swing application has been displayed, you are not allowed to access any of the components from anything that is not the event dispatching thread. With the exception of a few methods, like repaint(), it is not thread safe to do so.

Having a separate thread to handle the timing for changing the visibility is fine, but it is not allowed to directly manipulate the component. Take a look at the invokeLater() method of the SwingUtilities to dispatch that task back to the event dispatching thread.


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
i'm not sure if i got it right:
my main:

my refactored code:
It is sorta covered in the JavaRanch Style Guide.
subject: using threads or there's another way?