Hey so this is my first question I hope you understand my issue:
I have to develope a game with Java and JavaFX and I try to Update a Label from a FXML-created Gui. I try to bind the Label from the gui to a property from my model but if I debug the Code I get the Error:
Exception in thread "Thread-4" java.lang.IllegalStateException: Not on FX application thread; currentThread = Thread-4
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.access$000(StringPropertyBase.java:49)
at javafx.beans.property.StringPropertyBase$Listener.invalidated(StringPropertyBase.java:230)
at com.sun.javafx.binding.ExpressionHelper$SingleInvalidation.fireValueChangedEvent(ExpressionHelper.java:137)
at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:81)
at javafx.beans.binding.StringBinding.invalidate(StringBinding.java:171)
at com.sun.javafx.binding.BindingHelperObserver.invalidated(BindingHelperObserver.java:51)
at com.sun.javafx.binding.ExpressionHelper$SingleInvalidation.fireValueChangedEvent(ExpressionHelper.java:137)
at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:81)
at javafx.beans.property.IntegerPropertyBase.fireValueChangedEvent(IntegerPropertyBase.java:106)
at javafx.beans.property.IntegerPropertyBase.markInvalid(IntegerPropertyBase.java:113)
at javafx.beans.property.IntegerPropertyBase.set(IntegerPropertyBase.java:147)
at model.RegionModel.calculateCurrentEmission(RegionModel.java:199)
at model.RegionModel.run(RegionModel.java:46)
I try to understand the model-view-control model and at the moment my application starts on a little window with one Button "Start Game". If you click on this Button the scene changes to the game. This is the application everything starts with:
public class CeeOhh22Hell extends Application {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
//Needed method because it's already a JavaFX-Project
@Override
public void start(Stage primaryStage) throws Exception {
Hauptmenue start = new Hauptmenue(primaryStage);
primaryStage.show();
//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
This is the first scene:
public class mainmenu {
Stage primaryStage;
Button btn;
FXMLLoader loader;
public mainmenu(Stage primaryStage){
this.primaryStage = primaryStage;
btn = new Button("Start Game");
Group root = new Group(btn);
primaryStage.setScene(new Scene(root, 300,300));
btn.setOnAction(e -> {
try {
loader = new FXMLLoader(this.getClass().getResource("Region.fxml"));
Parent main = loader.load();
primaryStage.setScene(new Scene(main,1500,800));
Region_Controller contr = loader.getController();
} catch (IOException ex) {
System.out.println("Error");
}
});
}
The Controller:
public class Region_Controller implements Initializable {
RegionModel region;
@FXML
private ComboBox<?> settingsComBox;
@FXML
private Label playerName;
@FXML
private Label currentemission;
@FXML
private Label time;
@FXML
private Region gameRegion;
@FXML
private Button createCow;
/**
* Initializes the controller class.
*/
@Override
public void initialize(URL url, ResourceBundle rb) {
region = new RegionModel();
region.start();
playerName.setText("John Doe");
bindPropertys();
createCow.setOnAction(e -> {
addGameObjectToList(new Cow("Cow",generateFreeCoordinate()));
});
}
Pls keep in mind im just at the beginning to understand JavaFX and MVC. And sorry for my english. Thanks