Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

refresh label

 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How to refresh label with for loop?

 
Sheriff
Posts: 22647
126
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Where and how do you initialize your variable jLabel?
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
is this the same problem as your other post

https://coderanch.com/t/433458/Swing-AWT-SWT-JFace/java/Display-real-time
 
Ivan Kesic
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Michael Dunn wrote:is this the same problem as your other post

https://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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Michael, ill look into it
 
New rule: no elephants at the chess tournament. Tiny ads are still okay.
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic