我一直在使用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);
}
}
}
调用要创建的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();
}
我希望这有助于澄清事情。
答案 0 :(得分:0)
修正了它。我根据this教程重写了我的代码。
然后,我创建了一个BooleanProperty对象并为其添加了一个更改侦听器。当BooleanProperty设置为true时,执行创建切片的代码。然后,当我退出磁贴创建对话框时,我将BooleanProperty设置为true。
如果有人有任何疑问,我可以发布代码段。