Timer
Timer
is a facet designed to run some screen controller code at specified time intervals. It works in a thread that handles user interface events.
Therefore it can update screen components. Timer
stops working when a screen it was created for is closed.
Attributes
Timer
is defined in the facets
element of the screen XML descriptor and has the following attributes:
-
delay
- required attribute. Defines timer interval in milliseconds.
-
autostart
- optional attribute. The default value isfalse
, which means that the timer will start only when itsstart()
method is invoked. When it is set totrue
, the timer starts immediately after the screen opening.
-
repeating
- optional attribute. Turns on repeated executions of the timer. If the attribute is set totrue
, the timer runs in cycles at equal intervals defined in thedelay
attribute. Otherwise, the timer runs only once as many milliseconds as specified in the delay attribute after the timer start.
Events and Handlers
To generate a handler stub in Jmix Studio, select the facet in the screen descriptor XML or in the Component Hierarchy panel and use the Handlers tab of the Component Inspector panel. Alternatively, you can use the Generate Handler button in the top panel of the screen controller. |
TimerActionEvent
TimerActionEvent
allows you to execute some code at the time specified in the delay
attribute. If the repeating
attribute is set to true
, the code specified in this event will be executed periodically, until the timer is stopped.
@Subscribe("timer")
protected void onTimerFacetTick(Timer.TimerActionEvent event) {
seconds += event.getSource().getDelay() / 1000;
notifications.create(Notifications.NotificationType.TRAY)
.withCaption("Timer tick")
.withDescription(seconds + " seconds passed")
.show();
}
Programmatic registration of the event handler: use the addTimerActionListener()
component method.
TimerStopEvent
TimerStopEvent
allows you to execute some code after the timer is stopped.
@Subscribe("timer")
public void onTimerTimerStop(Timer.TimerStopEvent event) {
notifications.create(Notifications.NotificationType.TRAY)
.withCaption("Timer stopped")
.show();
}
Programmatic registration of the event handler: use the addTimerStopListener()
component method.
Usage Example
<window xmlns="http://jmix.io/schema/ui/window"
caption="msg://timerScreen.caption">
<facets>
<timer id="timer"
autostart="false"
delay="5000"
repeating="true"/> (1)
</facets>
<layout>
<hbox spacing="true"> (2)
<button id="startTimer"
caption="Start"
icon="PLAY"/>
<button id="stopTimer"
caption="Stop"
icon="STOP"/>
<label id="statusLabel"
align="MIDDLE_LEFT"
value="Timer is not running"/>
</hbox>
</layout>
</window>
1 | Defining timer in the facets element. |
2 | Defining the layout for controlling the timer. |
@Autowired
protected Timer timer;
@Autowired
protected Label<String> statusLabel;
@Autowired
protected Notifications notifications;
protected int seconds = 0;
@Subscribe("startTimer")
protected void onStartTimerClick(Button.ClickEvent event) {
timer.start(); (1)
statusLabel.setValue("Timer started");
notifications.create(Notifications.NotificationType.TRAY)
.withCaption("Timer started")
.show();
}
@Subscribe("timer")
protected void onTimerTick(Timer.TimerActionEvent event) {
seconds += event.getSource().getDelay() / 1000; (2)
notifications.create(Notifications.NotificationType.TRAY)
.withCaption("Timer tick")
.withDescription(seconds + " seconds passed")
.show();
}
@Subscribe("stopTimer")
protected void onStopTimerClick(Button.ClickEvent event) {
timer.stop(); (3)
seconds = 0;
statusLabel.setValue("Timer stopped");
notifications.create(Notifications.NotificationType.TRAY)
.withCaption("Timer stopped")
.show();
}
1 | Start the timer when the startTimer button is pressed. |
2 | Calculate how long the timer works. |
3 | Stopping the timer. |