-1

I have exeption in Timer whith use JavaFX.
timer imported of java.util
code of timer:

  @FXML private Label q1lt;    
  private final Timer t1=new Timer();
  TimerTask t2=new TimerTask(){
         int i = 300000;
         public void run() { 
             if(i==0){
                    file(prim+" "+b+"Балів","D://Результат.txt");
                    t1.cancel();
                    return;
                    }
                    i--;
                    int h,m,s;
                    h=i/360000;
                    m=i/6000%6000;
                    s=i%6000;   
                    q1lt.setText("Залишилось часу "+h+":"+m+":"+s/100);
                    }

                };
        t1.schedule(t2,10,10);   

void file:

public void file(String text,String fileName) {
     try {
            File file = new File(fileName);
            if(!file.exists()){
            file.createNewFile();
                 }
            PrintWriter out = new PrintWriter(file.getAbsoluteFile());
             try {
                     out.print(text);
                 } finally {
                     out.close();
                 }
             } catch(IOException exe) {
                 throw new RuntimeException(exe);
             }

code of exeption:

 Exception in thread "Timer-1" java.lang.IllegalStateException: Not on FX     application thread; currentThread = Timer-1
          at com.sun.javafx.tk.Toolkit.checkFxUserThread(Toolkit.java:236)
      at    com.sun.javafx.tk.quantum.QuantumToolkit.checkFxUserThread(QuantumToolkit.jav  a:423)
    at javafx.scene.Parent$2.onProposedChange(Parent.java:367)
    at com.sun.javafx.collections.VetoableListDecorator.setAll(VetoableListDecorator.java:113)
      at com.sun.javafx.collections.VetoableListDecorator.setAll(VetoableListDecorator.java:108)
    at com.sun.javafx.scene.control.skin.LabeledSkinBase.updateChildren(LabeledSkinBase.java:575)
    at com.sun.javafx.scene.control.skin.LabeledSkinBase.handleControlPropertyChanged(LabeledSkinBase.java:204)
    at com.sun.javafx.scene.control.skin.LabelSkin.handleControlPropertyChanged(LabelSkin.java:49)
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase.lambda$registerChangeListener$61(BehaviorSkinBase.java:197)
    at com.sun.javafx.scene.control.MultiplePropertyChangeListenerHandler$1.changed(MultiplePropertyChangeListenerHandler.java:55)
    at javafx.beans.value.WeakChangeListener.changed(WeakChangeListener.java:89)
    at com.sun.javafx.binding.ExpressionHelper$SingleChange.fireValueChangedEvent(ExpressionHelper.java:182)
    at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:81)
    at javafx.beans.property.StringPropertyBase.fireValueChangedEvent(StringPropertyBase.java:103)
    at javafx.beans.property.StringPropertyBase.markInvalid(StringPropertyBase.java:110)
    at javafx.beans.property.StringPropertyBase.set(StringPropertyBase.java:144)
      at javafx.beans.property.StringPropertyBase.set(StringPropertyBase.java:49)
    at     javafx.beans.property.StringProperty.setValue(StringProperty.java:65)
    at javafx.scene.control.Labeled.setText(Labeled.java:145)
    at ua.NazarRepyanskiy.Main$1.run(Main.java:313)
    at java.util.TimerThread.mainLoop(Unknown Source)
      at java.util.TimerThread.run(Unknown Source)

imported resurses:

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Timer;
import java.util.TimerTask;
import javafx.application.Application;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.*;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.control.RadioButton;
import javafx.scene.control.SingleSelectionModel;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.control.TextField;
import javafx.scene.layout.;

I can not use the platform and timeline.
So rewrite my code please. H e l p

1 Answers1

0

According to the exception you get, you call the q1lt.setText() method from the wrong thread. This method needs to be called from the JavaFX application thread:

    TimerTask t2=new TimerTask(){
    int i = 300000;
    public void run() {
        if(i==0){
            file(prim+" "+b+"Балів","D://Результат.txt");
            t1.cancel();
            return;
        }
        i--;
        int h,m,s;
        h=i/360000;
        m=i/6000%6000;
        s=i%6000;   
        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                q1lt.setText("Залишилось часу "+h+":"+m+":"+s/100);
            }
        });
    }
};
user7291698
  • 1,597
  • 2
  • 10
  • 29