I'm building app for making tests. I have a scene made in SceneBuilder. There is a ImageView under image Label with Question and 3 buttons "A", "B", "C", Texts to buttons and question's Label are taken from DataBase, if you click answer new Image, question and asnwers are loading, everything works, but i want to add a Timer in the corner. When image and question show on the screen there will be "Time to read the question" and countdown from 10 to 0, and then "Time to answer" and again countdown from 10 to 0. If timer ends and there is no answer question and image will change automaticly. But the problem is that, i can do the timer, its counting down, and after this time it change the question but I dont know how to put it into Label. If inside Timer I do something like seconds-- label.setText(seconds) there is no error but when I start app there is a lot of exceptions. Can you help me how I can put this variable which is decrementing in timer after each second to Label ?
public void setTimer() {
timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
if(interval > 0)
{
timeElapsed.setText("Time to read the question: "+interval);
System.out.println(interval);
interval--;
}
else
timer.cancel();
}
}, 1000,1000);
}
Now i have something like this, in console everything is working there is a countdown from 10 to 0 but no effect in scene.
And errors:
Exception in thread "Timer-0" java.lang.IllegalStateException: Not on FX application thread; currentThread = Timer-0
at javafx.graphics/com.sun.javafx.tk.Toolkit.checkFxUserThread(Toolkit.java:291)
at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.checkFxUserThread(QuantumToolkit.java:423)
at javafx.graphics/javafx.scene.Parent$3.onProposedChange(Parent.java:493)
at javafx.base/com.sun.javafx.collections.VetoableListDecorator.setAll(VetoableListDecorator.java:113)
at javafx.base/com.sun.javafx.collections.VetoableListDecorator.setAll(VetoableListDecorator.java:108)
at javafx.controls/javafx.scene.control.skin.LabeledSkinBase.updateChildren(LabeledSkinBase.java:271)
at javafx.controls/javafx.scene.control.skin.LabeledSkinBase.lambda$new$11(LabeledSkinBase.java:219)
at javafx.controls/com.sun.javafx.scene.control.LambdaMultiplePropertyChangeListenerHandler.lambda$new$1(LambdaMultiplePropertyChangeListenerHandler.java:49)
at javafx.base/javafx.beans.value.WeakChangeListener.changed(WeakChangeListener.java:89)
at javafx.base/com.sun.javafx.binding.ExpressionHelper$SingleChange.fireValueChangedEvent(ExpressionHelper.java:181)
at javafx.base/com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:80)
at javafx.base/javafx.beans.property.StringPropertyBase.fireValueChangedEvent(StringPropertyBase.java:104)
at javafx.base/javafx.beans.property.StringPropertyBase.markInvalid(StringPropertyBase.java:111)
at javafx.base/javafx.beans.property.StringPropertyBase.set(StringPropertyBase.java:145)
at javafx.base/javafx.beans.property.StringPropertyBase.set(StringPropertyBase.java:50)
at javafx.base/javafx.beans.property.StringProperty.setValue(StringProperty.java:65)
at javafx.controls/javafx.scene.control.Labeled.setText(Labeled.java:147)
at gui.controller.StudentTestYesNoController$1.run(StudentTestYesNoController.java:40)
at java.base/java.util.TimerThread.mainLoop(Timer.java:556)
at java.base/java.util.TimerThread.run(Timer.java:506)
The problem is that you are trying to change the UI from a thread other than the application.
This should solve the problems with your current implementation
public void setTimer() {
timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
if(interval > 0)
{
Platform.runLater(() -> timeElapsed.setText("Time to read the question: "+interval));
System.out.println(interval);
interval--;
}
else
timer.cancel();
}
}, 1000,1000);
}
Also, you can take a look at something specific about the javafx - Timeline
JavaFX periodic background task
Adding on to what mr mcwolf said above, i think you need to set
Platform.setImplicitExit(false);
before Platform.runLater(); so that it will run every time the task is called.
Related
I'm building a simple timer that will display the milliseconds as well. Something like
Hours:Minutes:Seconds:Milliseconds
I'm updating the TextView that displays the timer every millisecond, inside a Handler that calls itself every 1 millisecond:
Handler timerHandler = new Handler();
Runnable timerRunnable = new Runnable() {
#Override
public void run() {
//calculate new time
...
//update TextView
...
timerHandler.postDelayed(this, 1);
}
};
My question is, will calling a Handler every 1 millisecond significantly affect the battery usage of the app?
Is there any better approach (performance-wise) other than using a Handler?
I've checked multiple posts of timers and stopwatches implementations but they will either not display the milliseconds or they won't discuss about performance issues.
I'm trying to update a label every millisecond in libGDX for a set amount of time.
However, sometimes the label suddenly stops without an error OR I receive a "String index out of range" error which then crashes my program. These are two separate issues.
Code:
Stage stage;
Timer timer = new Timer();
Label countUpLabel;
int countUp;
#Override
public void create () {
stage = new Stage(new FitViewport(Gdx.graphics.getWidth(), Gdx.graphics.getHeight()));
//Setting up time label
BitmapFont font = new BitmapFont(Gdx.files.internal("04b_19__-32.fnt"));
LabelStyle labelStyle = new LabelStyle(font, Color.WHITE);
countUpLabel = new Label("Time:\n00000", labelStyle);
countUpLabel.setPosition(200,200);
countUpLabel.setAlignment(Align.center);
countUpLabel.setFontScale(3);
stage.addActor(countUpLabel);
//Setting up timer
timer.schedule(new TimerTask() {
#Override
public void run() {
if (countUp < 3000) {
countUp++;
countUpLabel.setText(String.format("Time:\n%d", countUp));
}else
timer.cancel();
}
}, 0, 1); //every millisecond run the timer
}
Thanks in advance.
Most of libGDX is not thread safe, unless explicitly stated that it is. Also, updating a label every millisecond, while it is only shown about every 16 milliseconds (the refresh rate of your typical display device), isn't a very good approach. So you might want to remove your timer and instead update the label when it is actually visible: in the render method:
float counter = 0f;
#Override
public void render() {
if (counter < 3) {
counter += Gdx.graphics.getDeltaTime();
countUpLabel.setText(String.format("Time:\n%01.22f", counter));
}
// call glClear, stage.act, stage.draw, etc.
}
Im programming a Music Player with a Seekbar.To manage it im using an Handler with a Runnable which does it update. Somehow it is lagging my UI . How can I stop this lag ?
OnCreate:
mHandler = new Handler();
When Im playing a Song:
public static void updateProgressBar() {
mHandler.postDelayed(mUpdateTimeTask, 100);
}
My Runnable:
private static Runnable mUpdateTimeTask = new Runnable() {
public void run() {
try {
long totalDuration = songService.getTotalDuration();
int currentDuration = songService.getCurrentDuration();
// Displaying Total Duration time
player_time_length.setText(""+utils.milliSecondsToTimer(totalDuration));
// Displaying time completed playing
player_time_current.setText(""+utils.milliSecondsToTimer(currentDuration));
// Updating progress bar
int progress = (int)(utils.getProgressPercentage(currentDuration, totalDuration));
SB_song.setProgress(currentDuration);
// Running this thread after 100 milliseconds
mHandler.postDelayed(this, 100);
}catch(Exception e){}
}
};
How can I prevent this lagg in my App ?
The lag is because the Runnable is executing in the UI Thread. To reduce or remove the lag, you have to reduce the amount of work you do inside the Runnable.
One thing that you can do is to remove long totalDuration = songService.getTotalDuration(); from the Runnable and instead, put it outside, like I do in my music player.
I could add more to this answer if you included the "utils" methods that you're using to convert milliseconds to human readable time.
I would say its because its running on the UI Thread which laggs. But you have to use UI elements so another Thread is not possible .. Im right ?
Make sure the text view is not set to wrap_content. That will trigger a layout pass each time you call setText.
So I have seven panels of different colors that need to be rotated in order. My code is working well for the most part but the first and last panels always have same color. How do I fix this?
I have already checked that each of my panels have a different color upon start.
Code:
public void run()
{
TimerTask colorAction = new TimerTask(){
public void run()
{
redPanel.setBackground(orangePanel.getBackground());
orangePanel.setBackground(yellowPanel.getBackground());
yellowPanel.setBackground(greenPanel.getBackground());
greenPanel.setBackground(bluePanel.getBackground());
bluePanel.setBackground(indigoPanel.getBackground());
indigoPanel.setBackground(violetPanel.getBackground());
violetPanel.setBackground(redPanel.getBackground());
}
};
java.util.Timer utilTimer = new java.util.Timer();
utilTimer.scheduleAtFixedRate(colorAction, START_AFTER, DELAY );
}
Snapshot (before change):
Snapshot (after change)
The basic problem, other then the fact that you are violating the single thread rules of Swing, is you are relying on a value from component whose background has already changed...
violetPanel.setBackground(redPanel.getBackground());
redPanel's background is now set to orangePanel background by the time you call this.
Instead, first grab redPanel's background color before you change anything, then apply it to violetPanel
Color redBackground = redPanel.getBackground();
redPanel.setBackground(orangePanel.getBackground());
//...
violetPanel.setBackground(redBackground);
Take a look (and get your teacher to do the same) at Concurrency in Swing and How to Use Swing Timers for more details...
If you MUST use a java.util.Timer, you should be wrapping your changes to UI in an invokeLater call, for example...
SwingUtilities.invokeLater(new Runnable() {
public void run() {
Color redBackground = redPanel.getBackground();
redPanel.setBackground(orangePanel.getBackground());
//...
violetPanel.setBackground(redBackground);
}
});
I've been trying for a while to understand the Handlers thingy in order to pause a game for a few seconds.. no luck so far.
I was hoping if someone could walk me through or show me a complete method that can be adapted to my needs.
which are:
I have a pong like game, when the ball hit either sides of the screen, I want to pause for a few seconds to display the score, and then to resume the game.
Thanks!
if you are using it would be easier (and more efficient) to use a library like libgdx or andengine to do this but to do it in your current case i suggest a simple solution
on the update method use a bool to know if the ball is paused
update()
{
if(!isPaused)
{
animateBall();
}
...
}
how to pause the ball
pauseBall()
{
isPaused=true;
Timer t= new Timer();
t.schedule(new TimerTask() {
#Override
public void run() {
isPaused = false;
}
}, fewSeconds);
}