Hello.

I started to learn javaFX recently and ran into a problem if the scene consists of 2 panels in different FXML, in the first there is a progress bar, in the second a button that launches updateProgress, but when it starts, a JavaFX Application Thread crashes. I would like to know what to do in this situation.

Main:

package testFX_2; import javafx.application.Application; import javafx.fxml.FXMLLoader; import javafx.scene.Scene; import javafx.scene.layout.VBox; import javafx.stage.Stage; import java.io.IOException; public class Main extends Application { private Stage stage; private VBox vBox; public static void main(String[] args) { launch(args); } @Override public void start(Stage stage) throws Exception { this.stage = stage; this.stage.setResizable(false); this.stage.setTitle("Test2"); rootPane(); secondPane(); } private void rootPane() throws IOException { FXMLLoader loader = new FXMLLoader(); loader.setLocation(Main.class.getResource("view/UI_main.fxml")); vBox = (VBox) loader.load(); Scene scene = new Scene(vBox); stage.setScene(scene); stage.show(); } private void secondPane() throws IOException { FXMLLoader loader = new FXMLLoader(); loader.setLocation(Main.class.getResource("view/UI_second.fxml")); VBox anchorPane = (VBox) loader.load(); vBox.getChildren().add(anchorPane); } } 

UI_mainController:

 package testFX_2.view; import javafx.fxml.FXML; import javafx.scene.control.ProgressBar; public class UI_mainController { @FXML public ProgressBar bar; } 

UI_main.fxml

 <?xml version="1.0" encoding="UTF-8"?> <?import javafx.scene.control.*?> <?import java.lang.*?> <?import javafx.scene.layout.*?> <VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="150.0" prefWidth="300.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="testFX_2.view.UI_mainController"> <children> <ProgressBar fx:id="bar" prefHeight="50.0" prefWidth="300.0" progress="0.0" /> </children> </VBox> 

UI_secondController:

 package testFX_2.view; import javafx.concurrent.Task; import javafx.fxml.FXML; import javafx.scene.control.Button; public class UI_secondController { @FXML private Button bar_btn; @FXML public void onClickBar(){ Task<Integer> task = new Task<Integer>() { @Override protected Integer call() throws Exception { int max = 1000; for (int i = 0; i <max; i++) { updateProgress(i,max); Thread.sleep(20); } return max; } }; UI_mainController main = new UI_mainController(); main.bar.progressProperty().bind(task.progressProperty()); new Thread(task).start(); } } 

UIsecond.fxml:

 <?xml version="1.0" encoding="UTF-8"?> <?import javafx.scene.control.*?> <?import java.lang.*?> <?import javafx.scene.layout.*?> <VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="101.0" prefWidth="300.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="testFX_2.view.UI_secondController"> <children> <Button fx:id="bar_btn" mnemonicParsing="false" onAction="#onClickBar" prefHeight="100.0" prefWidth="300.0" text="BAR" /> </children> </VBox> 

    1 answer 1

    Well, the very first remark - you should not create a UI_mainController. For you, this is done by javafx itself. What you need is to push the link to this controller, and from it already take the progress bar and bind

    There must be something like this

    Main.java

     public class Main extends Application { private UI_mainController firstController; ... private void rootPane() throws IOException { FXMLLoader loader = new FXMLLoader(); loader.setLocation(Main.class.getResource("view/UI_main.fxml")); vBox = (VBox) loader.load(); Scene scene = new Scene(vBox); this.firstController = loader.getController(); stage.setScene(scene); stage.show(); } private void secondPane() throws IOException { FXMLLoader loader = new FXMLLoader(); loader.setLocation(Main.class.getResource("view/UI_second.fxml")); VBox anchorPane = (VBox) loader.load(); loader.getController().setMainController(this.firstController); vBox.getChildren().add(anchorPane); } } 

    UI_secondController:

     package testFX_2.view; import javafx.concurrent.Task; import javafx.fxml.FXML; import javafx.scene.control.Button; public class UI_secondController { UI_mainController mainController; public void setMainController(MainController main) { this.mainController = main; } @FXML private Button bar_btn; @FXML public void onClickBar(){ Task<Integer> task = new Task<Integer>() { @Override protected Integer call() throws Exception { int max = 1000; for (int i = 0; i <max; i++) { Platform.runLater(() -> updateProgress(i,max)) Thread.sleep(20); } return max; } }; main.bar.progressProperty().bind(task.progressProperty()); new Thread(task).start(); } } 
    • Thank you so much, based on this example I added the code and everything works))) - Bleser