无法创建动态创建的JavaFX TilePane
我一直在使用FXML编写javafx程序。我试图从一个场景中提取的信息动态创建一个TilePane,但我似乎无法让TilePane显示出来。我已经尝试了我能想到的一切,但我被难倒了。我在下面发布相关代码。谢谢你的帮助 编辑:为了便于阅读,我简化了下面的代码。如果这还不够的话,我会做一个MCVE 主控制器无法创建动态创建的JavaFX TilePane,java,javafx,fxml,Java,Javafx,Fxml,我一直在使用FXML编写javafx程序。我试图从一个场景中提取的信息动态创建一个TilePane,但我似乎无法让TilePane显示出来。我已经尝试了我能想到的一切,但我被难倒了。我在下面发布相关代码。谢谢你的帮助 编辑:为了便于阅读,我简化了下面的代码。如果这还不够的话,我会做一个MCVE 主控制器 @FXML private ScrollPane gridScroll; private TilePane tPane; //Method to build each ce
@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);
}
}
}
我希望这有助于澄清问题。修复了它。我根据教程重写了代码 然后,我创建了一个BooleanProperty对象,并向其添加了一个更改侦听器。当BooleanProperty设置为true时,将执行创建平铺的代码。然后,当我退出平铺创建对话框时,我将BooleanProperty设置为true
如果有人有任何问题,我可以发布代码片段 可能的副本。我也看到了来自该线程的建议,但我仍然有问题。这篇文章已经更新以反映它们。我实际上看不出你的更新版本有什么问题,但是很难理解,代码也不完整。也许您可以创建一个与您描述的完全相同的阶段,即一个带有按钮和平铺窗格的阶段,该阶段打开一个新的FXML并填充平铺窗格,而不是更多,然后发布该阶段。至少,删除所有从未被引用的字段,例如InStitchesController中的primaryStage和stitchArray。我会在有空闲时间后立即执行此操作。非常感谢。
@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();
}