Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>
like image 278
simonides Avatar asked Oct 02 '22 01:10

simonides


1 Answers

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));
}
like image 144
James_D Avatar answered Oct 13 '22 12:10

James_D