2015-10-28 142 views
-1

加載GUI此函數加載我的「請稍候...」鬼:我怎樣才能顯示JavaFX的

public void showLoading(Stage ownerStage){ 
try { 
     FXMLLoader loader = new FXMLLoader();loader.setLocation(MainApp.class.getResource("/de/ern/loading/Loading.fxml")); 
     AnchorPane page = (AnchorPane) loader.load(); 

     LoadingController controller = loader.getController(); 
     controller.setLoadingText("Please wait..."); 
     waitStage = new Stage(); 
     waitStage.setTitle("Please wait"); 
     waitStage.getIcons().add(new Image(getClass().getResourceAsStream("logo.png")));//setzt das Icon 
     waitStage.initModality(Modality.WINDOW_MODAL); 
     waitStage.initOwner(ownerStage); 
     Scene scene = new Scene(page); 
     waitStage.setScene(scene); 
     waitStage.show(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

我怎麼有稱呼它,因爲當我直接調用它,它總是空的。只顯示標題窗格。我發現了很多Swing的代碼片段,但不是javafx。

編輯:

我把它從主界面的這樣的控制器:

mainApp.showLoading(primaryStage); 
AllgemeinTab liveWerte = new AllgemeinTab(this); 
liveWerte.setName(getAllgTabName()); 
liveWerte.setBeschreibungDe(getAllgTabBeschreibungDe()); 

,並在這之後,緊跟着一個大型數據庫交易。

這是我FXML文件:

<?xml version="1.0" encoding="UTF-8"?> 
<?import javafx.scene.text.*?> 
<?import javafx.scene.image.*?> 
<?import java.lang.*?> 
<?import javafx.scene.layout.*?> 
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="64.0" prefWidth="292.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="de.ernst.loading.LoadingController"> 
<children> 
<ImageView fx:id="imgLoading" fitHeight="25.0" fitWidth="25.0" layoutX="20.0" layoutY="20.0" pickOnBounds="true" preserveRatio="true" AnchorPane.bottomAnchor="19.0" AnchorPane.leftAnchor="20.0" AnchorPane.topAnchor="20.0"> 
    <image> 
     <Image url="@loading.gif" /> 
    </image> 
    </ImageView> 
    <Text layoutX="54.0" layoutY="25.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Status:" AnchorPane.topAnchor="17.0"> 
    <font> 
     <Font name="System Bold" size="12.0" /> 
    </font></Text> 
    <Text fx:id="txtLoadingDescription" layoutX="54.0" layoutY="24.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Beschreibung..." wrappingWidth="230.44140625" AnchorPane.topAnchor="30.0" /> 
</children> 
</AnchorPane> 
+1

創建併發布[MCVE]。您發佈的代碼似乎沒有任何問題。 –

+0

你可以粘貼你的fxml文件嗎? – ttarczynski

回答

0

您可以顯示「加載GUI」通過顯示ProgressIndicator

所以顯示ProgressIndicator,那麼你需要做的Backgroundwork在一個新的線程,然後你用新的內容替換您ProgressIndicator。

final ProgressIndicator progress = new ProgressIndicator(); 
progress.setMaxSize(50, 50); 
rootPane.setCenter(progress); 

new Thread(new Runnable() { 

    @Override 
    public void run() { 
     try { 
      Thread.sleep(1000); 
     } catch (InterruptedException e) { 
      e.printStackTrace(); 
     } 

     Platform.runLater(new Runnable() { 

      @Override 
      public void run() { 
        rootPane.setCenter(new Label("done")); 
      } 
     }); 
    } 
}).start(); 
0

你可能想看看PreloaderOracle Doc)。 Preloader是一個非常小的應用程序,在應用程序加載之前可以加載一個Stage,您可以將某些信息發送到預加載器以顯示進度。

這可能會給您更多選擇