2017-06-01 53 views
1

プログラムの読み込みに時間がかかるので、読み込み中に読み込みウィンドウが表示されます。しかし、私が入れたローディングインジケータは、.show()を使用すると回転しませんが、何らかの理由で.showAndWait()を使用すると動作します。 問題が何であるか把握できないようです。ここにはローディングウィンドウを呼び出すコントローラがあります。JavaFXのロードインジケータがアニメーション表示されていない場合

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()で呼び出され、イニシャライザで作成されます。 はまた、私は

loadingStage.initStyle(StageStyle.UTILITY); 

を使用してつくれないために、Windowsのナビゲーションバーを取得しようとしましたが、それはプログラムをクラッシュさせるようです。

Finalyは、ここで私は私のfxmlsをロードするためにこれを使用しています私の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> 
+0

'GeneratePressed'はおそらくイベントハンドラメソッドなので、FXアプリケーションスレッドで呼び出されます。したがって、FXアプリケーションスレッドが他のことをする前に完了します。したがって、実際にウィンドウを表示するには、 'loadStage.show()'と 'loadingStage.close()'の間に十分な時間がないか、FXアプリケーションスレッドをブロックして、UIをレンダリングできないようにします。窓が見えません。実行時間の長いコードをお持ちの場合は、バックグラウンドスレッドで実行する必要があります。 [Task'のAPIドキュメント](http://docs.oracle.com/javase/8/javafx/api/javafx/concurrent/Task.html)を参照してください。 –

答えて

0

です。あなたは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; 
} 

おそらくこれが役に立ちます。他Methodeの使用からアクセスするには

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()); 
     } 
    }); 

あなたは待っていない場合は、あなたのJavaがさらに実行されているので、これはMethodeのをsetOnSucceeded必要です。これが役立つかもしれない

関連する問題