An SO User
An SO User

Reputation: 25028

Measure the length of text in JavaFX

Here is a simple program that I wrote to have a button on the stage over a rounded rectangle background.

package definitive_guide;

import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Toolkit;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.SceneBuilder;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonBuilder;
import javafx.scene.layout.Region;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.StackPaneBuilder;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.shape.RectangleBuilder;
import javafx.scene.text.Font;
import javafx.stage.Stage;

public class SingleButton extends Application {
    Button btn;
    Scene scene;
    StackPane stack;
    Rectangle roundedRectangle;

    @Override
    public void start(Stage stage) throws Exception {
        btn = ButtonBuilder
                .create()
                .font(Font.font("DejaVu Sans Mono",22))
                .text("Hello World")
                .build();

        roundedRectangle = RectangleBuilder
                .create()
                .width(300)
                .height(300)
                .arcHeight(150)
                .arcWidth(200)
                .fill(Color.rgb(128, 128, 128))
                .build();

        stack = StackPaneBuilder
                .create()
                .children(roundedRectangle , btn)
                .build();
        stack.setMaxSize(Region.USE_PREF_SIZE, Region.USE_PREF_SIZE);

        scene = SceneBuilder
                .create()
                .root(stack)
                .build();

        stage.setScene(scene);
        stage.show();


    }


    public static void main(String[] args) {
        Application.launch("definitive_guide.SingleButton.java");
    }

}  

For now, the values for the length , breadth and the arcs of the rectangle are manually entered. So, they are not dynamic.

I want to be able to set its size depending upon the size of the text plus some padding so as to have a consistent look and feel across devices of different resolutions.
In Swing, FontMetrics was handy but I do not know what to do in JavaFX.

How do I do that?

Upvotes: 0

Views: 2394

Answers (1)

Uluk Biy
Uluk Biy

Reputation: 49215

If you really concerned with Text, try this:

final Text text = new Text("Click me");
text.setFont(Font.font("DejaVu Sans Mono", 42));

text.setOnMouseClicked(new EventHandler<MouseEvent>() {
    @Override
    public void handle(MouseEvent event) {
        // set with long text
        text.setText("Hello Sun, Hello Galaxy, Hello Space!");
    }
});

text.layoutBoundsProperty().addListener(new ChangeListener<Bounds>() {
    @Override
    public void changed(ObservableValue<? extends Bounds> observable, Bounds oldValue, Bounds newValue) {
        if(oldValue.getHeight() != newValue.getHeight() || oldValue.getWidth()!= newValue.getWidth()) {
            roundedRectangle.setWidth(newValue.getWidth() + 100);
            roundedRectangle.setHeight(newValue.getHeight() + 30);
        }
    }
});

Else if the text of button is your interest, you can try bindings to button directly:

roundedRectangle.widthProperty().bind(btn.widthProperty().add(100));
roundedRectangle.heightProperty().bind(btn.heightProperty().add(30));

The arc width and height values are also bindable:

roundedRectangle.arcHeightProperty()
roundedRectangle.arcWidthProperty()

Upvotes: 1

Related Questions