Note that there are some explanatory texts on larger screens.

plurals
  1. POJavaFx GridPane - how to center elements
    text
    copied!<p>I have a <code>GridPane</code> filled with 1-letter-labels.</p> <p>Here is an image:</p> <p><a href="http://s1.directupload.net/images/121010/xu8o79sr.jpg" rel="nofollow noreferrer">image http://s1.directupload.net/images/121010/xu8o79sr.jpg</a></p> <p>Here is the code:</p> <pre><code>int charSpacing = 1; int charsInWidth = 28; int charsInHeight = 16; double charWidth = 15; double charHeight = 20; GridPane gp = new GridPane(); gp.setAlignment(Pos.CENTER); Label[] tmp = new Label[charsInHeight*charsInWidth]; String text = "W"; int currArrPos = 0; for(int y = 0; y &lt; charsInHeight; y++) { HBox hbox = new HBox(charSpacing); for(int x = 0; x &lt; charsInWidth; x++) { tmp[currArrPos] = new Label(text); tmp[currArrPos].setTextFill(Paint.valueOf("white")); tmp[currArrPos].setMinHeight(charHeight); tmp[currArrPos].setMinWidth(charWidth); tmp[currArrPos].setMaxHeight(charHeight); tmp[currArrPos].setMaxWidth(charWidth); tmp[currArrPos].setStyle("-fx-border-color: white;"); hbox.getChildren().add(tmp[currArrPos++]); if(x%2 == 0){ text = "I"; } else{ text = "W"; } } gp.add(hbox, 1, y); } guiDisplay.getChildren().add(gp); </code></pre> <p>How can I center the characters? </p> <p>I have put them in a <code>HBox</code> and gave them spacing. I tried to make the <code>textAlignment</code> of the label to <code>CENTER</code>, but that doesn't work of course. </p> <p>I tried this also:</p> <pre><code>gp.setAlignment(Pos.CENTER); </code></pre> <p>Has anybody an idea? Thanks!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload