Skip to main content
removeu 1301 caracteres do conteúdo
Fonte Link
package chronometer;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;

public class FXMLDocumentController implements Initializable {
    
    @FXML    
    private Label lblMainChronometer;

    boolean validData() {
        if (intervals >= 0) {
            if (cadence.length() == 4) {
                try {
                    for (int i = 0; i < 4; i++) {
                        Integer.parseInt(Character.toString(cadence.charAt(i)));
                    }
                    return true;
        } catch(java.lang.NumberFormatException e) {
                    return false;
        } 
            }
        }
        return false;
    }
    
    private int intervals;   
    
    String intervalsTipe() {
        if (intervals == 0) {
            return "nonexistent";
        } else if (intervals <= 5) {
            return "short";
        } else if (5 < intervals && intervals <= 60) {
            return "normal";
        } else {
            return "long";
        }
    }
    
    private String cadence;
    
    private int seriesTime() { 
        if (cadence.length() == 4) { 
            int seriesTime = Integer.parseInt(Character.toString(cadence.charAt(0)));
            seriesTime += Integer.parseInt(Character.toString(cadence.charAt(1)));
            seriesTime += Integer.parseInt(Character.toString(cadence.charAt(2)));
            seriesTime += Integer.parseInt(Character.toString(cadence.charAt(3)));
            return seriesTime;
        }
        return 0;
    }
    
    private static int startTime;
    
    private void startCounter() { startTime = (int) System.currentTimeMillis(); }
    
    private static int currentTimeHundredth() {
        double currentTimeMillis;
        currentTimeMillis = startTime - System.currentTimeMillis();
        int currentTimeHundreth = (int) Math.floor(currentTimeMillis / 10);
        return currentTimeHundreth; 
    }
    
    private Runnable mainChronometer = new Runnable() {
        @Override
        public void run() {
            while (true) {
                
                int secondsHundredth = currentTimeHundredth() % 100;
                int currentTimeSeconds = currentTimeHundredth() - secondsHundredth;
                int seconds = (int) (currentTimeSeconds % 60);
                int minutes = (int) (currentTimeSeconds / 60);
                minutes %= 60; // to stay just the minutes (excluding the hours)           
                int hours = (int) (minutes / 60);
                lblMainChronometer.setText(String.format("%02d:%02d:%02d.%02d", hours, minutes, seconds, secondsHundredth));
                
            }
        }
    };
          
    @FXML
    private void handleButtonAction(ActionEvent event) throws InterruptedException {
        
        startCounter();
        new Thread(mainChronometer).start();
    }
    
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        
    }    
    
}
package chronometer;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;

public class FXMLDocumentController implements Initializable {
    
    @FXML    
    private Label lblMainChronometer;

    boolean validData() {
        if (intervals >= 0) {
            if (cadence.length() == 4) {
                try {
                    for (int i = 0; i < 4; i++) {
                        Integer.parseInt(Character.toString(cadence.charAt(i)));
                    }
                    return true;
        } catch(java.lang.NumberFormatException e) {
                    return false;
        } 
            }
        }
        return false;
    }
    
    private int intervals;   
    
    String intervalsTipe() {
        if (intervals == 0) {
            return "nonexistent";
        } else if (intervals <= 5) {
            return "short";
        } else if (5 < intervals && intervals <= 60) {
            return "normal";
        } else {
            return "long";
        }
    }
    
    private String cadence;
    
    private int seriesTime() { 
        if (cadence.length() == 4) { 
            int seriesTime = Integer.parseInt(Character.toString(cadence.charAt(0)));
            seriesTime += Integer.parseInt(Character.toString(cadence.charAt(1)));
            seriesTime += Integer.parseInt(Character.toString(cadence.charAt(2)));
            seriesTime += Integer.parseInt(Character.toString(cadence.charAt(3)));
            return seriesTime;
        }
        return 0;
    }
    
    private static int startTime;
    
    private void startCounter() { startTime = (int) System.currentTimeMillis(); }
    
    private static int currentTimeHundredth() {
        double currentTimeMillis;
        currentTimeMillis = startTime - System.currentTimeMillis();
        int currentTimeHundreth = (int) Math.floor(currentTimeMillis / 10);
        return currentTimeHundreth; 
    }
    
    private Runnable mainChronometer = new Runnable() {
        @Override
        public void run() {
            while (true) {
                
                int secondsHundredth = currentTimeHundredth() % 100;
                int currentTimeSeconds = currentTimeHundredth() - secondsHundredth;
        int seconds = (int) (currentTimeSeconds % 60);
        int minutes = (int) (currentTimeSeconds / 60);
        minutes %= 60; // to stay just the minutes (excluding the hours)           
        int hours = (int) (minutes / 60);
        lblMainChronometer.setText(String.format("%02d:%02d:%02d.%02d", hours, minutes, seconds, secondsHundredth));
                
            }
        }
    };
          
    @FXML
    private void handleButtonAction(ActionEvent event) throws InterruptedException {
        
        startCounter();
        new Thread(mainChronometer).start();
    }
    
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        
    }    
    
}
package chronometer;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;

public class FXMLDocumentController implements Initializable {
    
    @FXML    
    private Label lblMainChronometer;

    private static int startTime;
    
    private void startCounter() { startTime = (int) System.currentTimeMillis(); }
    
    private static int currentTimeHundredth() {
        double currentTimeMillis;
        currentTimeMillis = startTime - System.currentTimeMillis();
        int currentTimeHundreth = (int) Math.floor(currentTimeMillis / 10);
        return currentTimeHundreth; 
    }
    
    private Runnable mainChronometer = new Runnable() {
        @Override
        public void run() {
            while (true) {
                
                int secondsHundredth = currentTimeHundredth() % 100;
                int currentTimeSeconds = currentTimeHundredth() - secondsHundredth;
                int seconds = (int) (currentTimeSeconds % 60);
                int minutes = (int) (currentTimeSeconds / 60);
                minutes %= 60; // to stay just the minutes (excluding the hours)           
                int hours = (int) (minutes / 60);
                lblMainChronometer.setText(String.format("%02d:%02d:%02d.%02d", hours, minutes, seconds, secondsHundredth));
                
            }
        }
    };
          
    @FXML
    private void handleButtonAction(ActionEvent event) throws InterruptedException {
        
        startCounter();
        new Thread(mainChronometer).start();
    }
    
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        
    }    
    
}
Fonte Link

Estou tentando fazer um programa de cronometro usando java FXML no scene builder e não consigo

Toda vez que clico no botão, na tentativa de iniciar o cronometro, esse erro acontece:

Exception in thread "Thread-4" java.lang.IllegalStateException: Not on FX application thread; currentThread = Thread-4

referindo-se a essa linha de código : `

lblMainChronometer.setText(String.format("%02d:%02d:%02d.%02d", hours, minutes, seconds, secondsHundredth));

Este é o FXMLDcumentControler do meu programa :

package chronometer;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;

public class FXMLDocumentController implements Initializable {
    
    @FXML    
    private Label lblMainChronometer;

    boolean validData() {
        if (intervals >= 0) {
            if (cadence.length() == 4) {
                try {
                    for (int i = 0; i < 4; i++) {
                        Integer.parseInt(Character.toString(cadence.charAt(i)));
                    }
                    return true;
        } catch(java.lang.NumberFormatException e) {
                    return false;
        } 
            }
        }
        return false;
    }
    
    private int intervals;   
    
    String intervalsTipe() {
        if (intervals == 0) {
            return "nonexistent";
        } else if (intervals <= 5) {
            return "short";
        } else if (5 < intervals && intervals <= 60) {
            return "normal";
        } else {
            return "long";
        }
    }
    
    private String cadence;
    
    private int seriesTime() { 
        if (cadence.length() == 4) { 
            int seriesTime = Integer.parseInt(Character.toString(cadence.charAt(0)));
            seriesTime += Integer.parseInt(Character.toString(cadence.charAt(1)));
            seriesTime += Integer.parseInt(Character.toString(cadence.charAt(2)));
            seriesTime += Integer.parseInt(Character.toString(cadence.charAt(3)));
            return seriesTime;
        }
        return 0;
    }
    
    private static int startTime;
    
    private void startCounter() { startTime = (int) System.currentTimeMillis(); }
    
    private static int currentTimeHundredth() {
        double currentTimeMillis;
        currentTimeMillis = startTime - System.currentTimeMillis();
        int currentTimeHundreth = (int) Math.floor(currentTimeMillis / 10);
        return currentTimeHundreth; 
    }
    
    private Runnable mainChronometer = new Runnable() {
        @Override
        public void run() {
            while (true) {
                
                int secondsHundredth = currentTimeHundredth() % 100;
                int currentTimeSeconds = currentTimeHundredth() - secondsHundredth;
        int seconds = (int) (currentTimeSeconds % 60);
        int minutes = (int) (currentTimeSeconds / 60);
        minutes %= 60; // to stay just the minutes (excluding the hours)           
        int hours = (int) (minutes / 60);
        lblMainChronometer.setText(String.format("%02d:%02d:%02d.%02d", hours, minutes, seconds, secondsHundredth));
                
            }
        }
    };
          
    @FXML
    private void handleButtonAction(ActionEvent event) throws InterruptedException {
        
        startCounter();
        new Thread(mainChronometer).start();
    }
    
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        
    }    
    
}

Me ajudem, por favor!!!