我正在尝试使用Java线程将秒表嵌入到我的应用程序中,但是我不得不面对一个异常,因此,由于这个原因,我现在将其堆叠。我是线程新手。此外,我使用JavaFX构建UI。 我想清楚地知道这里发生的事情像异常一样跳跃。
当我调用名为“ actionTakeOrder”的动作时,我请求一些数据,并且它可以正常工作,没关系。在我通过它调用“ callStopWatch()”之后,我尝试启动一个用java线程构建的秒表,然后我必须面对如下异常,请注意异常块后的代码
例外
Exception in thread "Thread-5" java.lang.IllegalStateException: Not on FX application thread; currentThread = Thread-5
at com.sun.javafx.tk.Toolkit.checkFxUserThread(Toolkit.java:236)
at com.sun.javafx.tk.quantum.QuantumToolkit.checkFxUserThread(QuantumToolkit.java: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 controller.ChefController$1.run(ChefController.java:151)
Exception in thread "Thread-5" java.lang.IllegalStateException: Not on FX application thread; currentThread = Thread-5
at com.sun.javafx.tk.Toolkit.checkFxUserThread(Toolkit.java:236)
at com.sun.javafx.tk.quantum.QuantumToolkit.checkFxUserThread(QuantumToolkit.java: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 controller.ChefController$1.run(ChefController.java:152)
相同的异常一次又一次地重复
应用代码
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package controller;
import com.jfoenix.controls.JFXButton;
import com.jfoenix.controls.JFXCheckBox;
import connector.ProxyHandler;
import dto.CustomDTO;
import dto.OrderdetailDTO;
import static java.lang.Thread.sleep;
import java.net.URL;
import java.util.ArrayList;
import java.util.Optional;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.collections.FXCollections;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import service.custom.ChefService;
import service.custom.TelephoneOperatorService;
/**
* FXML Controller class
*
* @author kavindu
*/
public class ChefController extends Application implements Initializable {
static ChefService cs = ProxyHandler.getInstance().getChef();
@FXML
private AnchorPane panetophead;
@FXML
private Label lbtoid;
@FXML
private Label lbdate;
@FXML
private Label lbtime;
@FXML
private JFXButton btntakeorder;
@FXML
private Label lborder;
@FXML
private TableView<CustomDTO> tblorderdetails;
private CustomDTO currentdata;
@FXML
private JFXButton btnfinish;
@FXML
private JFXCheckBox cheackboxx;
// ====================================
private int milliseconds;
private int hours;
private int min;
private int seconds;
private static boolean state = true;
@FXML
private Label lbstopwatchhours;
@FXML
private Label lbstopwatchseconds;
@FXML
private Label lbstopwatchmin;
// ====================================
/**
* Initializes the controller class.
*/
@Override
public void initialize(URL url, ResourceBundle rb) {
tblorderdetails.getColumns().get(0).setCellValueFactory(new PropertyValueFactory<>("index"));
tblorderdetails.getColumns().get(1).setCellValueFactory(new PropertyValueFactory<>("itemid"));
tblorderdetails.getColumns().get(2).setCellValueFactory(new PropertyValueFactory<>("itemname"));
tblorderdetails.getColumns().get(3).setCellValueFactory(new PropertyValueFactory<>("qty"));
}
@FXML
private void actionTakeOrder(ActionEvent event) {
String orderid = null;
try {
CustomDTO order = cs.getOrder();
currentdata = order;
orderid = order.getOrderid();
lborder.setText(orderid);
ArrayList<CustomDTO> orderdetailarr = order.getCusdto();
System.out.println("tblorderdetails : "+orderdetailarr);
for (CustomDTO c : orderdetailarr) {
System.out.println("check order in controller :"+c.getIndex()+" , "+c.getItemid()+" , "+c.getItemname()+" , "+c.getQty());
}
tblorderdetails.setItems(FXCollections.observableArrayList(orderdetailarr));
} catch (Exception ex) {
Logger.getLogger(ChefController.class.getName()).log(Level.SEVERE, null, ex);
}
if (null != orderid) {
btntakeorder.setDisable(true);
callStopWatch();
}
}
private void callStopWatch(){
Thread t = new Thread(){
public void run(){
for (;;) {
if (true == state) {
try{
sleep(1);
if(milliseconds>1000){
milliseconds = 0;
seconds++;
}
if(seconds>60){
milliseconds = 0;
seconds = 0;
min++;
}
if(min>60){
milliseconds = 0;
seconds = 0;
min = 0;
hours++;
}
lbstopwatchseconds.setText(String.valueOf(seconds));
lbstopwatchmin.setText(String.valueOf(min));
lbstopwatchhours.setText(String.valueOf(hours));
milliseconds++;
}catch(Exception e){
}
}else{
break;
}
}
}
};
t.start();
}
@FXML
private void actionFinishTheOrder(ActionEvent event) {
state = false;
btntakeorder.setDisable(false);
Alert alert1 = new Alert(Alert.AlertType.CONFIRMATION);
alert1.setTitle("Confirmation for Finishing");
alert1.setHeaderText(null);
alert1.setContentText("Are you sure?");
Optional<ButtonType> result = alert1.showAndWait();
if (result.get() == ButtonType.OK){
System.out.println("Order finished..");
} else {
state = true;
callStopWatch();
}
}
// ====================main========================
public static void main(String[] args) {
launch(args);
}
public void start(Stage primaryStage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("../view/Chef.fxml"));
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.centerOnScreen();
primaryStage.show();
}
}