Win a copy of Classic Computer Science Problems in Swift this week in the iOS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Update JLabel with clock in Swing Java  RSS feed

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I want to make a clock in java swing and i did a label wich shows the time and I would like to update it ...I tried different methods but none of them work...can you please give me an ideea ?

 
Rancher
Posts: 2954
32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you try the Timer class?

none of them work


Can you post the code you are having problems with along with an explanation of the problems you have?
 
Serban Cameron
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Norm Radder
Rancher
Posts: 2954
32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok that is the first part, now what about the second part:
an explanation of the problems you have?

Also the code is missing the import statements and the class definiiton and main method so it can be compiled and executed to see the problem.
 
Serban Cameron
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the full code


And the problem is that the time does not update itself
 
Norm Radder
Rancher
Posts: 2954
32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

the time does not update itself


Is the run() method called?  What is the value of the variable: string that is used to set the text?

Add a print statement to see what string's value is.
 
Serban Cameron
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes i called the run method and printed the variable that sets the text and still nothing
 
Norm Radder
Rancher
Posts: 2954
32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

printed the variable that sets the text and still nothing


What is the value of string when you print it?  Can you copy the print out and paste it here so we can see what is printed?
 
Sheriff
Posts: 4832
135
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hints: Does cal.getTime() change from invocation to invocation?  Does where you declare cal have an effect on this?
 
Sheriff
Posts: 21308
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't use java.util.Timer in Swing, use javax.swing.Timer.
 
Serban Cameron
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I used javax.swing.timer and added this code



Still doesn`t do nothing...even thou the timer is running

Any ideas ?
 
Rancher
Posts: 3450
39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at Knute's hint.

A Calendar is not a clock, it represents a single date/time.
 
Norm Radder
Rancher
Posts: 2954
32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you add a print statement that printed out the value of string on line 51 as I suggested?  What was printed?
With the new code snippet you posted, try printing the value returned by getTime just before line 4.
 
Serban Cameron
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I solved it...topic closed..Thanks for your answers
 
Marshal
Posts: 58829
179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We have discussed your solution on another thread. I think it would be better to combine the two discussions.
 
Campbell Ritchie
Marshal
Posts: 58829
179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I merged your stuff with the following thread. I hope that is okay by you.
 
Serban Cameron
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I made a alarm clock in java i will post code here



And when hours and minutes are equal with the numbers from JTextField I want to do something...I made it do something but only if you put the hour and minute that you open the clock at it executes alarm but if i put next minute it wont do nothing..can you please help me
 
Saloon Keeper
Posts: 4413
108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not quite sure what the code is trying to accomplish, but the usual approach to scheduling in Java involves the java.util.Timer and TimerTask classes.
 
Serban Cameron
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried with Timer and it doesnt work...and with threads it works...the idea is that the first variables h m and s do not modify..they take a value and it does not change..i want them to change like the clock changes
 
Norm Radder
Rancher
Posts: 2954
32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

does not change..i want them to change like the clock changes


Can you add a print statement that prints out the values of the variables to show what is not changing?


What code is in the org.joda packages that are used in this program?
 
Campbell Ritchie
Marshal
Posts: 58829
179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adding threads to Swing is potentially dangerous, since Swing, like most GUI frameworks, is not thread‑safe.
You would use a Timer, but usually a Swing Timer, not java.util.Timer. The Swing times calls an action listener.

I also don't like the loops; you have an infinite loop, and a while loop whose duration I am not certain about.
 
Norm Radder
Rancher
Posts: 2954
32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@OtherHelpers  What is a good way to show the OP what his endless for loops are doing?
Or Should we just say BAD  and work together to find a better way?
 
Campbell Ritchie
Marshal
Posts: 58829
179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norm Radder wrote:. . . What is a good way to show the OP what his endless for loops are doing?

When you reach my age, you won't want to mess with infinite loops

Or Should we just say BAD  and work together to find a better way?

You mean like looking in the Java™ Tutorials for a section about Timers?
 
Norm Radder
Rancher
Posts: 2954
32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

When you reach my age,


Like not buying any green bananas.  I didn't think there were any others my age on the forum.  Looks like I registered here 2 months before you.
 
Campbell Ritchie
Marshal
Posts: 58829
179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norm Radder wrote:. . . Like not buying any green bananas. . . .

Bananas aren't even fit to eat until they turn brown. I once went to Central Africa and they had red bananas there: much nicer. But that loop will run longer than it takes for your bananas to ripen.

Looks like I registered here 2 months before you.

Yes, I think you did.
 
Serban Cameron
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Solved it with Timers thanks guys
 
Campbell Ritchie
Marshal
Posts: 58829
179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well done
But you mustn't solve it with bananas
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!