aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Message doesnt display continuously Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Message doesnt display continuously" Watch "Message doesnt display continuously" New topic
Author

Message doesnt display continuously

miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
i'm using the code bellow (written a couple of years ago) to display a message to a jLabel, for a few seconds
yet, the message does display but with "hicups", not permanently: for example - shows for 4 seconds, then disappears for a fraction of second, then resumes displaying...
i tried several combinations for sleep value and for var i, but the present combination looks like the best so far
is there something wrong with the code?

the code:


java amateur
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18990
    
  40

Swing isn't thread safe. You are not allowed to change swing components in any thread but the event dispatching thread.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Rok Štelcer
Ranch Hand

Joined: Nov 03, 2009
Posts: 101
Hi,

Any method call that affects the visualization in any way, should(must) be executed from the EDT.
In your case it's not.
The thing is, that you've just created a new worker/background thread on the EDT.
The same result would be achieved, if you'd comment the following lines: 3,4,21 & 22.

The "visual" call in your example is setText(...).
Put it on the EDT like:



Regards,
Rok


SCJP, SCWCD
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
this isnt the first time it happens: i rely on email notification and when i notice, several hours elapsed since first answer
anyway thank you both for your answers
using the (new) code bellow i dont perceive any irregularity in display
please fell free to comment what i wrote:
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18907
    
    8

That sort of thing is exactly what javax.swing.Timer is meant for. Try that instead.
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
thank you for the idea!
i came up to the following code, but for somw reason, when i hover out of rhe button that triggers the method, the message vanishes... i kmow i have some code for hovering, but with the other method that didnt happen

please comment in case you find something wrong
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19761
    
  20

Moving to our GUI forum.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Rok Štelcer
Ranch Hand

Joined: Nov 03, 2009
Posts: 101
Post deleted. ;)
Mikko Kohtamäki
Ranch Hand

Joined: Dec 13, 2008
Posts: 112
Are you trying to flash the text or what?


OR
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
Mikko: i'm trying not to flash
anyway, among others, i get an error message using your code in line 12, saying:
the method sleep(int) is undefined for the type new Runnable(){}
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19761
    
  20

It's a static method in Thread.
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
i'm feeling a bit embarrassed because i cant write it in order to compile:

Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problems:
Syntax error on token ";", { expected after this token
Syntax error, insert "}" to complete ClassBody
Syntax error, insert ";" to complete BlockStatements

at util.Util.mostraMensagem(Util.java:92)
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19761
    
  20

You're missing a }.
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
Rob Prime wrote:You're missing a }.

yes, that's what the compiler says
but i just cant figure out where, because if i added it here, gives an error there, and viceversa, and so on - believe me i tried a lot indeed, wtill with no results
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19761
    
  20

Line 25 here ends the while loop, and line 26 ends the anonymous inner class. You don't close the run method.
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
the code below does compile and run, at last
thanks to everyone!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Message doesnt display continuously