JavaFX label contents not showing in gridpane


Question

Why is myLabel content not showing in my gridpane, it is strange, everything else is showing..

I have tried everything I can. any help please

here are snippets of my code and attached photo of the result

enter image description here

@Override public void start(Stage stage) {

        Group root = new Group();
        Scene scene = new Scene(root, 1180, 650);
        stage.setScene(scene);
        stage.setTitle("Prayer Time Display");
        scene.getStylesheets().addAll(this.getClass().getResource("style.css").toExternalForm());

        GridPane Mainpane = new GridPane();
        scene.setRoot(Mainpane);
        Mainpane.setGridLinesVisible(true);
        Mainpane.setId("Mainpane");

        GridPane prayertime_pane = new GridPane();
        prayertime_pane.setId("prayertime_pane");
        prayertime_pane.setPadding(new Insets(20, 20, 20, 20));
        prayertime_pane.setAlignment(Pos.BASELINE_CENTER);
        prayertime_pane.setVgap(7);
        prayertime_pane.setHgap(35);

        GridPane moon_pane = new GridPane();
        moon_pane.setGridLinesVisible(true);
        moon_pane.setPadding(new Insets(10, 10, 10, 10));
        moon_pane.setAlignment(Pos.BASELINE_CENTER);
        moon_pane.setVgap(10);
        moon_pane.setHgap(10);

        myLabel.setText("hello");
        moon_pane.setConstraints(myLabel, 0, 0);
        moon_pane.getChildren().add(myLabel);

        ImageView Moon_img = new ImageView(new Image(getClass().getResourceAsStream("/Images/Full_Moon.png")));    
        Moon_img.setFitWidth(100);
        Moon_img.setFitHeight(100);
        moon_pane.add(Moon_img, 1, 0);

        Text next_moon_text = new Text("13/02");
        next_moon_text.setId("prayer-text-english");
        moon_pane.setHalignment(next_moon_text,HPos.LEFT);
        moon_pane.setValignment(next_moon_text,VPos.CENTER);
        moon_pane.setConstraints(next_moon_text, 2, 0);
        moon_pane.getChildren().add(next_moon_text);

        Mainpane.add(moon_pane, 8, 1,3,2);
1
1
8/29/2013 9:25:32 AM

Accepted Answer

Your label does appear on the scene but it has been shrinked to the extent that only the text overflow characters are rendered - "..."

To ensure that the label is always rendered with a fixed width that will fit all the text you can force the minumum and maximum widths to be the same as the preferred width:

  myLabel.setMinWidth(Region.USE_PREF_SIZE);
  myLabel.setMaxWidth(Region.USE_PREF_SIZE);

If you want to retain the dynamic sizing you can always use Scenic View (http://fxexperience.com/scenic-view/) to analyse your scene graph and determine which nodes or containers take up all the space or have size limits

1
8/29/2013 11:47:35 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon