0

I have been working on a javafx program using FXML. I am trying to dynamically create a TilePane from information pulled in from one scene, and I can't seem to get the TilePane to show up. I've tried just about everything I can think of, and I am stumped. I am posting the relevant code below. Thank you for your help!

EDIT: I've simplified the code below for easier reading. If this isn't enough, I'll make an MCVE.

Main Controller

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

}

Code from the secondary controller that calls the tilepane to be created

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

}

I hope this helps clarify things.

Risky_91
  • 81
  • 3
  • 9
  • possible duplicate of [JavaFX FileChooser Throws Error (probably easy fix, but still confused)](http://stackoverflow.com/questions/23094846/javafx-filechooser-throws-error-probably-easy-fix-but-still-confused). Also see http://stackoverflow.com/questions/23105433/javafx-8-compatibility-issues-fxml-static-fields – James_D Sep 22 '14 at 20:32
  • Implemented the suggestions from that thread, but I'm still having issues. The post has been updated to reflect them. – Risky_91 Sep 22 '14 at 21:48
  • I can't actually see anything wrong with your updated version, but it's quite hard to follow and the code is not complete. Perhaps you could create a [MCVE](http://stackoverflow.com/help/mcve) that does just what you are describing (i.e. a stage with a button and a tile pane, that opens a new FXML and populates the tile pane) and no more, and post that. At a minimum, remove all the fields that are never referenced, such as `primaryStage` and `stitchArray` in `InStitchesController`. – James_D Sep 23 '14 at 00:22
  • I will do that as soon as I get some free time. Thank you. – Risky_91 Sep 23 '14 at 02:26

1 Answers1

0

Fixed it. I rewrote my code based on this tutorial.

Then, I created a BooleanProperty object and added a change listener to it. When the BooleanProperty was set to true, the code to create tiles was executed. Then, I set the BooleanProperty to true when I exited the tile creation dialog.

If anyone has any questions, I can post code snippets.

Risky_91
  • 81
  • 3
  • 9