Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

refresh label

 
Ivan Kesic
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to refresh label with for loop?

 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where and how do you initialize your variable jLabel?
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Michael, ill look into it
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic