Scaling a textbox without truncating text within it


Question

I'm trying to create a simple "Page" where a piece of text is bounded within a rectangle(the page). I want to be able to put a large amount of text within the rectangle, if the text does not fit within the rectangle then it is scaled down? Is this possible?

Thanks

1
1
11/2/2012 9:29:55 AM

import javafx.application.Application;
import javafx.geometry.Bounds;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class Test extends Application {

    @Override
    public void start( Stage stage ) throws Exception {
        double width = 100;
        // create rectangle of specified width
        Rectangle r = new Rectangle( 90, 200, width, 20 );
        // get bounds of the rectangle
        Bounds rb = r.getBoundsInLocal();
        // create two identical texts for comparison
        Text t1 = new Text(50, 170, "Some long text that would not fit.");
        Text t2 = new Text(50, 190, "Some long text that would *** fit.");
        // get bounds of the text
        // this will return how much space the text actually takes
        Bounds t2b = t2.getBoundsInLocal();
        // find the scale down amount
        double scalex = rb.getWidth()/t2b.getWidth();
        // scale the text down to what the rect is
        t2.setScaleX( scalex );

        Pane root = new Pane();
        root.getChildren().addAll( r, t1, t2 );

        stage.setScene( new Scene( root, 800, 600 ) );
        stage.show();
    }

    public static void main( String args[] ) {
        launch( args );
    }
}
3
11/9/2012 1:07:35 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon