Reputation:
I wonder if JLabel really can't display a value beside String and Icon. For example for displaying Clock variable..
Clock clock = Clock.systemDefaultZone();
And the JLabel setText()
method as following..
label.setText(clock);
I'm aware that setText()
method only works with String parameter. But since clock variable can't be converted to String, i really have no idea how to display it on the label. Or what should i use instead of JLabel to display Clock variables.
I tried label.setText(Clock.valueOf(clock));
, until later i realized that only works for primitive data (or no?). I really stuck at this moment.
Upvotes: 0
Views: 54
Reputation: 797
I'm going to assume you're using java.time.Clock
. You can convert the current time into an ISO-8601 string by doing clock.instant().toString()
.
Therefore, you can put the clock in the label using:
label.setText(clock.instant().toString());
If you want the label to show the clock updating, you may need to use something like a Timer to reset the label text every second.
You can change the formatting if you don't want ISO-8601 with this to set your own format.
Upvotes: 2
Reputation: 1776
JLabel will set the text to an object if you have overridden the toString() method and call to it..
As
System.out.println(clock); //this would automatically call the toString() method.
For this.. You would have to directly call it.
label.setText(clock.toString());
Upvotes: 0