JavaFX TextAlignment Gotcha
Here is a minimal test case illustrating how exasperating the default JavaFX Text node can be, especially when aligning text.
You’d be surprised to note that both pieces of text below are aligned using TextAlignment.CENTER.
import javafx.scene.text.*;
import javafx.scene.layout.*;
VBox
{
translateY:10
content:
[
Text {
textAlignment:TextAlignment.CENTER
content: "A New World"
}
,
Text {
textAlignment:TextAlignment.CENTER
content: "A Mighty PubnwithnAbsolutely No Bitter Beer"
}
]
}
Apparently, the state of the art work-around is to calculate the mid-point using myTextNode.boundsInLocal.width, and then position using translateX.
Not good enough, Sun. Not good enough.
Here’s the worked around code
import javafx.scene.*;
import javafx.scene.text.*;
import javafx.scene.layout.*;
var scene:Scene;
scene=Scene
{
content:
{
VBox
{
translateY:10
content:
[
Text {
id: "label1"
translateX: bind (scene.width-scene.lookup("label1").boundsInLocal.width)/2
textAlignment:TextAlignment.CENTER
content: "A New World"
},
Text {
id: "label2"
translateX: bind (scene.width-scene.lookup("label2").boundsInLocal.width)/2
textAlignment:TextAlignment.CENTER
content: "A Mighty PubnwithnAbsolutely No Bitter Beer"
},
]
}
}
}

No comments
Jump to comment form | comments rss [?] | trackback uri [?]