简体   繁体   中英

JavaFX Thread with progressindicator not spinning, work done in non FXThread

I would like to indicate a loading process with the JavaFX progressindicator. The problem is that the indicator isn't rotating when the code is executed the first time. On the second time it is rotating that means the bindings are working, both the disable and the visible-property.

I also change the state in the FX thread and do the work on a seperate thread so i can see no error here.

Does anyone see the problem?

Controller:

//vars
@FXML private ProgressIndicator loadingIndicator;
private BooleanProperty isSaving = new SimpleBooleanProperty(false);

@Override
public void initialize(URL location, ResourceBundle resources) {
    parentToDisable.disableProperty().bind(isSaving);
    loadingIndicator.visibleProperty().bind(isSaving);
}


@FXML
void onSave(ActionEvent event) {
    isSaving.set(true);            //<<<<<<<<<problem
    // separate non-FX thread
    new Thread() {
        // runnable for that thread
        public void run() {

//++++++++++//long running task......+++++++++++++++

            // update ProgressIndicator on FX thread
            Platform.runLater(new Runnable() {
                public void run() {
                    isSaving.set(false);  //<<<<<<<<<problem
                }
            });
        }
    }.start();
}

Fxml:

<ScrollPane fitToHeight="true" fitToWidth="true" prefHeight="600.0"
    prefWidth="500.0" xmlns="http://javafx.com/javafx/8" 
    xmlns:fx="http://javafx.com    
    /fxml/1">
    <content>
        <StackPane>
            <children>
                <VBox fx:id="parentToDisable">
                    <!-- shortened -->
                    <Button fx:id="btnSave" mnemonicParsing="false" onAction="#onSave"
                        text="Speichern" />
                    <!-- shortened -->
                </VBox>
                <ProgressIndicator fx:id="loadingIndicator"
                    maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity"
                    minWidth="-Infinity" prefHeight="60.0" prefWidth="60.0" 
                    visible="false" />
            </children>
        </StackPane>
    </content>
</ScrollPane>

That looks like a bug: RT-33261 . It seems to be fixed in the latest pre-release of JDK 8u20.

My guess is it's to do with the progress indicator not being visible when the scene graph is first constructed. As a workaround, remove the visible="false" attribute from the ProgressIndicator in the fxml file, and wrap the binding in a Platform.runLater(...) call:

public void initialize(...) {
    Platform.runLater(() -> loadingIndicator.visibleProperty().bind(isSaving));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM