Fandom

Planet JFX

Centering Text

118pages on
this wiki
Add New Page
Talk0 Share

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.

Summary Edit

On the mailing list was a discussion of how to center text in nodes.

Centered-text

Code Edit

import javafx.ui.*;
import javafx.ui.canvas.*;

var contentText = "Sardegna"; //very nice island, go there once ;-)
var myFont = Font {face: VERDANA, style: [BOLD], size: 50};

Frame {
    content:
    Canvas {
        var: canvas
        height: 70
        width: 450
        border: null
        content:
        Group {
            var w = bind canvas.width
            var h = bind canvas.height
            content:
            [Rect {
                width: bind w
                height: bind h
                fill: LinearGradient {
                    x1: 0
                    y1: 0
                    x2: 1
                    y2: 0
                    stops:
                    [Stop {offset: 0, color: green},
                    Stop {offset: .5, color: new Color(.5, 1, 0, 1)},
                    Stop {offset: 1, color: green}]
                }
                stroke: green
                strokeWidth: 1
            },
            Text {
                transform: bind translate(w/2, h/2)
                //verticalAlignment: BASELINE
                valign: MIDDLE
                halign: CENTER
                content: contentText
                font: myFont
                fill: darkgreen
            }]
        }
    }
}

Also on Fandom

Random Wiki