Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Aligning transformed Text (and other shapes) inside StackFrame

NikolayArtamonovSep 22 2013 — edited Sep 22 2013

Good day, guys! I have StackPane and scaled (enlarged with scaleX and scaleY properties) Text node inside pane. When text aligned to center of pane (Pic. 1) there are no issues. But when I try to align to left, right, or any other sides or corners then text positioned outside of pane (Pic. 2). How can I fix it? Text have TextBoundsType.VISUAL bounds. Thanks!

Link to image...

Pic. 1. Grey text aligned to center of stack pane.

StackPane.setAlignment(Pos.CENTER)

Link to image...

Pic. 2. Grey text aligned to left side of stack pane.

StackPane.setAlignment(Pos.CENTER_LEFT)

This post has been answered by MoC on Sep 22 2013
Jump to Answer

Comments

MoC

Can you increase the font size instead of scaling the Text?

-fx-font-size: 2.0em;

This would fix the issue, I think.

user8929955

The StackPane will center everything you put into it. Use a Group instead.

NikolayArtamonov

Yes, it will fix the issue, but I need proportional scaling of text relative to size of stack pane. By the way, size of stack pane isn't fixed and may change on some conditions. It's hard to do with font size, that's why I use scaleX/Y properties.

MoC
Answer

You could try wrapping the Text in a Group. I tested in Scene Builder and it seemed to work ok with different StackPane alignments:

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>

<?import java.util.*?>

<?import javafx.scene.*?>

<?import javafx.scene.layout.*?>

<?import javafx.scene.paint.*?>

<?import javafx.scene.text.*?>

<StackPane alignment="TOP_LEFT" prefHeight="230.0" prefWidth="335.0" style="" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2">

  <children>

    <Group id="Group">

      <children>

        <Text layoutX="-29.0" layoutY="-11.0" scaleX="2.0" scaleY="2.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Text" />

      </children>

    </Group>

  </children>

</StackPane>

Marked as Answer by NikolayArtamonov · Sep 27 2020
shakir.gusaroff

Hi. Try the following:

text.setTextAlignment(TextAlignment.LEFT);

StackPane.setAlignment(Pos.CENTER);

NikolayArtamonov

Thank you very much, MoC! Wrapping text to group gracefully solved the problem! )

1 - 6
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Oct 20 2013
Added on Sep 22 2013
6 comments
442 views