Text “Alignment” Trick

One of the annoying sharp edges in JavaFX is around text alignment, especially with regards to the Text node. In this tip, I’m going to skip using the Text node in favor of using the Label control, because it gives you better… uh… control over text placement. This is actually based on a trick I used to use with Swing all the time. If I wanted to center some text in a layout, I’d specify the space I wanted the text positioned within by setting the size of the label to match that space, and then I’d either position the text at the center, top, right, etc.

Using the same trick in JavaFX, I can cause some text to be right positioned. An additional benefit to using the Label control is that if the space isn’t big enough for the text, it can be elided using the textOverrun variable.

    Label {
        width: bind 120
        text: "Right Positioned Message!"
        font: Font { size: 10 }
        textFill: Color.BLACK
        hpos: HPos.RIGHT

Anyway, it ain’t perfect but a good trick for the moment.

JavaFX links of the week, July 5

Wow, July already. Time sure is flying these days. As always, please feel free to flick me an email or a tweet to let me know of any news you want included. Let’s get on with the news!

That’s it for another week folks. Catch you in a weeks time.