• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Message doesnt display continuously

 
miguel lisboa
Ranch Hand
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Henry Wong
author
Marshal
Pie
Posts: 21446
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Swing isn't thread safe. You are not allowed to change swing components in any thread but the event dispatching thread.

Henry

 
Rok Štelcer
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
miguel lisboa
Ranch Hand
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21322
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That sort of thing is exactly what javax.swing.Timer is meant for. Try that instead.
 
miguel lisboa
Ranch Hand
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20613
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to our GUI forum.
 
Rok Štelcer
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Post deleted. ;)
 
Mikko Kohtamäki
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you trying to flash the text or what?


OR
 
miguel lisboa
Ranch Hand
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20613
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's a static method in Thread.
 
miguel lisboa
Ranch Hand
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20613
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're missing a }.
 
miguel lisboa
Ranch Hand
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20613
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the code below does compile and run, at last
thanks to everyone!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic