【问题标题】:Unable to create a dynamically created JavaFX TilePane无法创建动态创建的 JavaFX TilePane
【发布时间】:2014-11-16 21:31:51
【问题描述】:

我一直在使用 FXML 开发一个 javafx 程序。我正在尝试根据从一个场景中提取的信息动态创建一个 TilePane,但我似乎无法让 TilePane 显示出来。我已经尝试了几乎所有我能想到的东西,但我很难过。我在下面发布相关代码。感谢您的帮助!

编辑:我已经简化了下面的代码以便于阅读。如果这还不够,我会制作一个 MCVE。

主控制器

@FXML   private ScrollPane gridScroll;
        private TilePane tPane;

//Method to build each cell for the tilepane
private StackPane buildCell(Stitch stitch){

    StackPane pane = new StackPane();
    pane.setPrefSize(5, 5); 

    // add labels to stackpane
    Label stitchLabel = new Label(stitch.getStitchType().toString()); 
    Label strandLabel = new Label(stitch.getNumStrands().toString()); 

    //create rectangle to color stackpane
    Rectangle rect = new Rectangle (5, 5); //set rectangle to same size as stackpane
    rect.setFill(stitch.getDisplayColor()); //Color the rectangle
    rect.setStroke(Color.BLACK);    //border the rectangle in black

    pane.getChildren().addAll(stitchLabel, strandLabel, rect); 

    return pane;
}

protected void createTiles(Stitch[][] stitchArray, int width, int height){

    tPane = new TilePane(); //Create a new tilepane
    gridScroll.setContent(tPane); //add tilepane to existing scrollpane

    tPane.setPrefColumns(width); //set prefcolumns to the array width

    //add cells to tilepane
    for (int i=0; i<width; i++){
        for (int j=0; j<height; j++){
            StackPane cell = buildCell(stitchArray[i][j]);
            tPane.getChildren().add(cell);
        }
    }

}

来自二级控制器的代码,用于调用要创建的平铺窗格

  @FXML void finish(){

    //create data to populate tilepane with
    Stitch[][] stitchArray = floss.ImageConversion.createStitchArray(posterizedImage); 
    int width = (int) currentPicWidth; //cast double to int
    int height = (int) currentPicHeight; //cast double to int

    //get the main Controller
    FXMLLoader loader = new FXMLLoader(getClass().getResource("InStitchesFXML.fxml"));
    try {
        loader.load();
    } catch (IOException e) {e.printStackTrace();} 

    InStitchesController isCtrl = loader.getController();

    //create the tiles
    isCtrl.createTiles(stitchArray, width, height);

    //close the stage
    importStage.close();

}

我希望这有助于澄清事情。

【问题讨论】:

  • 实施了该线程的建议,但我仍然遇到问题。该帖子已更新以反映它们。
  • 我实际上看不出您的更新版本有什么问题,但是很难理解并且代码不完整。也许您可以创建一个 MCVE 来完成您所描述的工作(即一个带有按钮和一个平铺窗格的舞台,它会打开一个新的 FXML 并填充平铺窗格),然后再发布。至少,删除所有从未引用的字段,例如InStitchesController 中的primaryStagestitchArray
  • 我一有空就会这样做。谢谢。

标签: java javafx fxml


【解决方案1】:

修复它。我根据this 教程重写了我的代码。

然后,我创建了一个 BooleanProperty 对象并为其添加了一个更改侦听器。当 BooleanProperty 设置为 true 时,将执行创建图块的代码。然后,当我退出图块创建对话框时,我将 BooleanProperty 设置为 true。

如果有人有任何问题,我可以发布代码 sn-ps。

【讨论】:

    猜你喜欢
    • 2020-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-13
    • 2020-01-22
    • 1970-01-01
    • 2017-01-02
    • 2020-06-23
    相关资源
    最近更新 更多