This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Java in General and the fly likes Regarding use of methods in Swing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Regarding use of methods in Swing" Watch "Regarding use of methods in Swing" New topic
Author

Regarding use of methods in Swing

Raghuraman Muthuswamy
Ranch Hand

Joined: Mar 18, 2003
Posts: 73
Dear All,
I am having a swing progam. Now i want to add a method getTime() which uses Calendar class for getting time. I want to add the time in my JLabel. How can i do that. Can you send me the code snippet for achieving this. Your help is very much appreciated.

Thanks in advance,

Regards,
Raghu
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Calendar.getInstance() will give you a Calendar object with the current time and then you can use the DateFormat class to format the time anyway you like.

The JLabel has a setText method that takes a String so once you have used the DateFormat class to get the time as String, you can use the setText method to put the time in the JLabel.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Raghuraman Muthuswamy:
Dear All,
I am having a swing progam. Now i want to add a method getTime() which uses Calendar class for getting time. I want to add the time in my JLabel. How can i do that. Can you send me the code snippet for achieving this. Your help is very much appreciated.

Thanks in advance,

Regards,
Raghu


What have you tried on your own?

Layne


Java API Documentation
The Java Tutorial
Raghuraman Muthuswamy
Ranch Hand

Joined: Mar 18, 2003
Posts: 73
Dear Thomas,
I have used the Calendar.getInstance() to get Hour, Minute and Seconds. I converted them to String and as per your sugesstion, used the setText method of JLabel to display the time in JLabel. Thanks for your sugesstion and will post more queries.

regards,
Raghu
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Raghuraman Muthuswamy:
Dear Thomas,
I have used the Calendar.getInstance() to get Hour, Minute and Seconds. I converted them to String and as per your sugesstion, used the setText method of JLabel to display the time in JLabel. Thanks for your sugesstion and will post more queries.

regards,
Raghu


For future reference, it will help A LOT if you post some code when you ask questions. That way we can help with the specific changes you need to make. Also, if you need help resolving any compiler errors, you also need to post the full error message so we can help you.

Keep Coding!

Layne
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Originally posted by Raghuraman Muthuswamy:
Thanks for your sugesstion and will post more queries.


Glad I could help!

 
Consider Paul's rocket mass heater.
 
subject: Regarding use of methods in Swing