我一直在使用FXML处理javafx程序。我试图从一个场景中拉入的信息动态创建一个TilePane,我似乎无法让TilePane出现。我尝试过所有我能想到的事情,而且我很难过。我在下面发布相关代码。感谢您的帮助!无法创建动态创建的JavaFX 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);
}
}
}
从调用tilepane副控制器编码创建
@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();
}
我希望这有助于澄清事情。
可能重复的[JavaFX FileChooser抛出错误(可能易于修复,但仍然困惑)](http://stackoverflow.com/questions/23094846/javafx-filechooser-throws-error-probably-easy-fix-but-仍然困惑)。另请参阅http://stackoverflow.com/questions/23105433/javafx-8-compatibility-issues-fxml-static-fields – 2014-09-22 20:32:53
从该线程实施建议,但我仍然有问题。该帖子已更新以反映它们。 – 2014-09-22 21:48:40
我实际上看不到您的更新版本有任何问题,但很难遵循,代码也不完整。也许你可以创建一个[MCVE](http://stackoverflow.com/help/mcve),它可以完成你正在描述的内容(例如一个带有按钮和平铺窗格的平台,它将打开一个新的FXML并填充平铺窗格)并没有更多,并张贴。至少,删除所有从未引用的字段,例如'InStitchesController'中的'primaryStage'和'stitchArray'。 – 2014-09-23 00:22:08