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

refresh label

Ivan Kesic
Ranch Hand

Joined: Feb 05, 2009
Posts: 68
How to refresh label with for loop?

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19696
    
  20

Where and how do you initialize your variable jLabel?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
is this the same problem as your other post

http://www.coderanch.com/t/433458/Swing-AWT-SWT-JFace/java/Display-real-time
Ivan Kesic
Ranch Hand

Joined: Feb 05, 2009
Posts: 68
Michael Dunn wrote:is this the same problem as your other post

http://www.coderanch.com/t/433458/Swing-AWT-SWT-JFace/java/Display-real-time


Yes it almost is! But i am not sure that that other post is understandible! Maybe i should write this as a new question there?
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
it would be better to post your code so we can compile/run and see exactly what you are doing.
don't post your entire code, strip all unrelated stuff - menu's and other components not in the 'flow' of the problem.

make sure, after stripping, it compiles, runs, and produces the same problem - then post that
Ivan Kesic
Ranch Hand

Joined: Feb 05, 2009
Posts: 68
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
it's because of the way you call loop()
you create a new Gui(), then call loop(), so your new Gui() is effectively initialized like this

where labLabel is null

to fix, change the button's actionPerformed to this


Ivan Kesic
Ranch Hand

Joined: Feb 05, 2009
Posts: 68
Thank you very much!

But i still have a big problem!
I implanted sleep so its now like a timer! But, it only update, refresh labLabel Text at the end of the loop, ofcourse number 9, not every turn of it.(if i put System.out.println(i) it works fine in console, so i figure it has something to do with jlabel refreshing)

i tryed to put labLabel.repaint(); below abLabel.setText("Number: "+i); but nothing is happening.

[
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
your thread is not going to do anything - it has no run()

read this (particularly updating swing components via SwingUtilities.invokeLater(..))

http://java.sun.com/javase/6/docs/api/javax/swing/package-summary.html#threading

the easiest way for you to do what you want is to use a Swing Timer (does all the background threading for you)
you could also use a SwingWorker
Ivan Kesic
Ranch Hand

Joined: Feb 05, 2009
Posts: 68
Thank you Michael, ill look into it
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: refresh label