2016-07-06 65 views
0

我正在處理需要VBoxes的「表格」表示的項目。我對應用程序的分層佈局是GridPane - > VBox(在其中一個單元格) - > VBoxes(顯示不同數據集在彼此之上) - >數據。我有兩個場景。JavaFX:動態添加VBox不顯示

數據顯示在場景1上。用戶可以通過表單添加數據並單擊場景2上的按鈕。然後,添加的數據應顯示在現有數據下面,作爲父視圖中的VBox 1再次。

下面是代碼,這將使它明確:

我的場景1 .fxml文件看起來如下(簡體):

<GridPane fx:id="grid" fx:controller="application.Controller"> 
    [here: ColumnConstraints] 
    <children> 
    <VBox fx:id="parentBox" GridPane.columnIndex="0" GridPane.rowIndex="1"/> 
    <Button fx:id="goToScene2" text="+" onAction="#goToScene2"/> 
    </children> 
</GridPane> 

場景2只是有一個按鈕和一個文本字段:

<GridPane fx:id="grid" fx:controller="application.AddDataController"> 
    [here: ColumnConstraints] 
    <children> 
    <Button fx:id="addData" text="add" onAction="#bAddData"/> 
    <TextField fx:id="data"/> 
    </children> 
</GridPane> 

我的場景1控制器(Controller)是這樣的:

public class Controller implements Initializable { 
    @FXML Button goToScene2; 
    @FXML VBox parentBox; 

    @Override 
    public void initialize(URL location, ResourceBundle resources) { 
    } 

    public void addData(String s) { 
    Label lbl = new Label(s); 
    VBox dataBox = new VBox(); 
    dataBox.setPadding(new Insets(15, 5, 15, 5)); 
    dataBox.setSpacing(5); 
    dataBox.setMaxHeight(80); 
    dataBox.getChildren().add(lbl); 
    parentBox.getChildren().add(dataBox); 
    } 
} 

這是設計的,因爲dataBox包含的元素多於標籤,但在這種情況下,這與我看起來並不相關。

我的場景2控制器(addDataController)看起來是這樣的:

@FXML Button addData; 
@FXML TextField data; 

@FXML protected void bAddData(){ 
    String content = data.getText();  
    FXMLLoader fxmlLoader = new FXMLLoader(); 
    Pane p = fxmlLoader.load(getClass().getResource("scn1.fxml").openStream()); 
    Controller cont = (Controller) fxmlLoader.getController(); 

    cont.addData(content); 
} 

所以,當一個人點擊在場景2的附加數據按鈕,觸發的方法輸入的數據傳送到現場的控制器這是因爲現在應該在場景1中顯示新數據。

覺得這樣的邏輯不工作(這裏編輯),因爲當我之前並添加數據後索要

System.out.println(parentBox.getChildren().size(); 

,它總是有一個單一的兒童,即使它應該有一個更多...

如果我人爲地填充字符串數組並將所有從addData到(String s)的所有內容移動到Initialize(...),它都可以工作,並且數據在父VBox中顯示爲VBox。

我沒有發佈主類,因爲加載控制器和場景變化不是問題。

非常感謝大家的幫助! :)

+1

我不相信你用來調用'AddData(content)'的'Controller const'實例與''Scene'正在使用的'Controller'的實例是同一個實例。 FXMLLoader正在創建一個不在場景圖中的新實例。一個簡單的修復可能是將主場景控制器傳遞到第二個場景。你的'goToScene'方法是什麼樣的? – OttPrime

+0

這實際上是我沒有見過的方式。但是,所使用的Controller是在.fxml文件中定義的,所以我無法從任何其他類訪問它。它從未在JavaFX代碼中加載過。使控制器類靜態也沒有工作。 – s4inz

+0

對不起。 'goToScene'方法在主程序類中調用一個方法並傳遞一個String。該字符串在switch-case語句中處理,並觸發'currentStage.setScene(newScene)'。 – s4inz

回答

0

只是提供更詳細的信息,連同一個想法,我不知道如何實施。

這是我的主類:

@Override 
public void start(Stage primaryStage) { 
    currentStage = primaryStage; 
    Parent root = FXMLLoader.load(getClass().getResource("Scene1.fxml")); 
    scene1 = new Scene(root); 
} 

我能在這一點上加載控制器並作出通過控制器的吸?所以我在整個程序中只有一個實例。

我試着插入:

FXMLLoader fxmlLoader = new FXMLLoader(); 
fxmlLoader.load(getClass().getResource("Scene1.fxml").openStream()); 
Controller controller = (Controller) fxmlLoader.getController(); 

Parent root ... 

行之後。但是,這是兩次加載.xml文件。我怎樣才能很好地連接兩個部分?

非常感謝您耐心等待!

編輯::::::::: 下面的代碼爲我工作:現在

@Override 
public void start(Stage primaryStage) { 
    currentStage = primaryStage; 
    FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("Scene1.fxml")); 
    Parent root =(Parent) fxmlLoader.load(); 
    controller = (Controller) fxmlLoader.getController(); 
} 

,我可以問的主類controller,它總是通過該單個實例。

謝謝你指點我正確的方向! :)