JavaFX加载指示器在显示时未设置动画

时间:2017-06-01 02:19:13

标签: java user-interface javafx

我的程序需要一些时间才能加载,因此在加载时,我会显示加载窗口。但是,当我使用.show()时,我输入的加载指示器不会旋转,但出于某种原因,在使用.showAndWait()时会起作用。 我似乎无法弄清问题是什么。这是调用加载窗口的控制器。

package FX;

import Utility.*;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

import java.io.IOException;

/**
 * Created by Alex on 5/31/2017.
 */
public class WindowController {

@FXML private TextField startBox;
@FXML private Button genButton;
@FXML private ComboBox startPage;
@FXML private ComboBox endPage;
@FXML private GridPane grid;
private Stage loadingStage;
private Scene loadingScene;

private Graph graph;

@FXML
public void initialize()throws IOException{
    AnchorPane pane = FXMLLoader.load(getClass().getResource("LoadingScreen.fxml"));

    loadingScene = new Scene(pane);
    loadingStage = new Stage();
    loadingStage.setTitle("Loading");
    loadingStage.setResizable(false);
    loadingStage.setScene(loadingScene);
    //loadingStage.initStyle(StageStyle.UTILITY);
}

@FXML
public void GeneratePressed() throws IOException{
    loadingStage.show();
    String page = startBox.getText();
    graph = new Graph(page);

    ObservableList<String> list = graph.getNamesList();
    startPage.setItems(list);
    endPage.setItems(list);
    loadingStage.close();
}

@FXML void FindPathPressed(){

}

public void dislayLoading(){

}

}

该窗口在GeneratePressed()中调用,并在初始化程序中创建。 此外,我试图通过使用

使Windows导航栏不是apear
loadingStage.initStyle(StageStyle.UTILITY);

但这似乎使程序崩溃。

最后,这是我的fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.ProgressIndicator?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>


<AnchorPane maxHeight="180.0" maxWidth="250.0" minHeight="180.0" 
minWidth="250.0" prefHeight="180.0" prefWidth="250.0" 
xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" 
fx:controller="FX.LoadingScreenController">
   <children>
      <Text layoutX="87.0" layoutY="52.0" strokeType="OUTSIDE" 
strokeWidth="0.0" text="Loading" textAlignment="CENTER">
     <font>
        <Font name="System Bold" size="20.0" />
     </font>
  </Text>
  <ProgressIndicator fx:id="load" layoutX="87.0" layoutY="74.0" 
prefHeight="72.0" prefWidth="76.0" />
   </children>
</AnchorPane>

1 个答案:

答案 0 :(得分:0)

我正在使用它来加载我的fxmls。您不需要fxml,也不需要只有一个窗格。

/*
 * Loader for custom FXML on ScrollPane
 * You doesn't need the parameter, you can replace it
 * with your own fix path if you only load one view.
 */
Task<Parent> loadFXML(String path) {
    //Loader Task
    Task<Parent> createMainScene = new Task<Parent>() {
        @Override
        public Parent call() {
            //Update Message
            updateMessage("Loading...");

            //Pane with FXML
            Pane pane = null;

            try {
                // FXML loader
                FXMLLoader loader = new FXMLLoader();

                // Set path to PARAMETERLISTE.fxml
                loader.setLocation(getClass().getResource(path));

                // Set load fxml to Pane
                pane = (Pane) loader.load();
            } catch (Exception e) {
                System.err.println("Error. Grund: " + e.getMessage());
            }

            //Return Pane
            return pane;
        }
    };

    //ProgressBar
    ProgressBar pBar = new ProgressBar();
    //Load Value from Task
    pBar.progressProperty().bind(createMainScene.progressProperty());
    //Set PrefWidth
    pBar.setPrefWidth(250);
    //New Loading Label
    Label statusLabel = new Label();
    //Get Text
    statusLabel.textProperty().bind(createMainScene.messageProperty());
    //Set Style to Label
    statusLabel.setStyle("-fx-font: 24 arial;");

    //Layout
    VBox root = new VBox(statusLabel, pBar);
    //SetFill Width TRUE
    root.setFillWidth(true);
    //Center Items
    root.setAlignment(Pos.CENTER);

    //Set Layout to ScrollPane
    scrollpane.setContent(root);

    //Display loaded FXML onSucceed
    createMainScene.setOnSucceeded(e -> {
        scrollpane.setContent(createMainScene.getValue());
    });

    //Start Thread
    new Thread(createMainScene).start();

    return createMainScene;
}

也许这会对你有所帮助。您可以使用以下方式设置自定义进度:

updateProgress([YOUR PROGRESS], [MAX VALUE]);

要从其他方法使用它:

    //Load yFXML
    Task<Parent> task = loadFXML("/YOURFXML.fxml");
    //Set on Succeeded
    task.setOnSucceeded(new EventHandler<WorkerStateEvent>() {
        @Override
        public void handle(WorkerStateEvent arg0) {
            //Do your Stuff

            //Set Scrollpane to new FXML
            scrollpane.setContent(task.getValue());
        }
    });

你需要这个setOnSucceeded方法,因为如果你不等待,你的java会进一步运行。愿这有帮助