简体   繁体   English

JavaFX:动态添加的VBox不会显示

[英]JavaFX: Dynamically added VBox does not show up

I'm working on a project that requires a "tabled" representation of VBoxes. 我正在一个需要VBoxes的“表格”表示形式的项目中。 My hierarchical layout of the application is GridPane -> VBox (in one of the Cells) -> VBoxes (that display different datasets on top of each other) -> Data. 我对应用程序的分层布局是GridPane-> VBox(在一个单元中)-> VBoxes(在彼此之上显示不同的数据集)->数据。 I have two Scenes. 我有两个场景。

Data is displayed on Scene 1. The user can add data through a form and by clicking a button on Scene 2. Then, the added data should be displayed below the existing data as a VBox within the parent-VBox on Scene 1 again. 数据显示在场景1上。用户可以通过表单并单击场景2上的按钮来添加数据。然后,添加的数据应再次作为场景1的父VBox内的VBox显示在现有数据下方。

Here is the code that will make it clear: 这是清楚的代码:

My Scene 1 .fxml file looks the following (Simplified): 我的Scene 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>

Scene 2 just has a button and a TextField: 场景2仅具有一个按钮和一个TextField:

<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>

My Scene 1 controller (controller) looks like this: 我的Scene 1控制器(控制器)如下所示:

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

This is designed as it is because the dataBox contains more elements than the label, but that doesn't seem relevant to me in this context. 这是按原样设计的,因为dataBox包含的元素比标签更多,但是在这种情况下与我似乎无关。

My Scene 2 controller (addDataController) looks like this: 我的Scene 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);
}

So, when one clicks on the Add-Data-Button in Scene 2, the triggered method passes the entered data to the Controller of Scene 1. This is because the new data should be displayed in Scene 1 now. 因此,当单击场景2中的“添加数据按钮”时,触发的方法会将输入的数据传递给场景1的控制器。这是因为现在应该在场景1中显示新数据。

I feel like the logic does not work (edited here), because when I ask for 我觉得逻辑不起作用(在此处编辑),因为当我要求

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

before and after the data was added, it always has one single Child, even though it should have one more... 在添加数据之前和之后,它始终只有一个孩子,即使应该再有一个孩子...

If I artificially fill a String-Array and move everything from addData to(String s) to Initialize(...), it does work and the data shows up as VBoxes in the parent-VBox. 如果我人为填充字符串数组并将所有内容从addData移到(String s)到Initialize(...),它将起作用,并且数据在父VBox中显示为VBoxes。

I didn't post the main class, because loading Controllers and Scene change is not an issue. 我没有发布主课程,因为加载控制器和场景更改不是问题。

Thank you all very very much for your help! 非常感谢大家的帮助! :) :)

Just to provide more detailed information, together with an idea, that I don't know how to implement. 为了提供更详细的信息以及一个我不知道如何实现的想法。

This is my main class: 这是我的主要课程:

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

Could I load the controller at this point and make a getter that passes the Controller? 我可以在此时加载控制器并使其通过控制器的吸气剂吗? So I'd only have one single instance of it throughout the program. 因此,在整个程序中,我只有一个实例。

I tried inserting: 我尝试插入:

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

after the 之后

Parent root ...

line. 线。 But that's loading the .xml file twice. 但这将两次加载.xml文件。 How can I nicely connect both parts? 我怎样才能很好地连接两个部分?

Thanks so much for you patience! 非常感谢您的耐心等待!

EDIT::::::::: The following code worked for me: 编辑::::::::::以下代码为我工作:

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

Now, I can ask the main class for controller and it always passes that single instance. 现在,我可以向主类询问controller并且它始终会传递该单个实例。

Thank you for pointing me to the right direction! 感谢您为我指明正确的方向! :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM