如何在JavaFX中隐藏文本溢出?

时间:2018-03-16 10:39:37

标签: javafx-8

我在JavaFX中创建自定义文件选择器。这是一个按钮组:

这是一个fxml文件内容:

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

<?import javafx.geometry.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<GridPane hgap="5.0" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity"
          minWidth="-Infinity" xmlns:fx="http://javafx.com/fxml/1"
          xmlns="http://javafx.com/javafx/8.0.131">
    <columnConstraints>
        <ColumnConstraints hgrow="SOMETIMES" maxWidth="-Infinity" minWidth="200.0"
                           prefWidth="200.0"/>
        <ColumnConstraints hgrow="SOMETIMES" maxWidth="-Infinity" minWidth="25.0"
                           prefWidth="25.0"/>
        <ColumnConstraints hgrow="SOMETIMES" maxWidth="-Infinity" minWidth="25.0"
                           prefWidth="25.0"/>
    </columnConstraints>
    <rowConstraints>
        <RowConstraints vgrow="SOMETIMES"/>
    </rowConstraints>
    <children>
        <StackPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity"
                   minWidth="-Infinity" prefHeight="25.0" prefWidth="200.0"
                   style="-fx-background-color: lightgrey;" GridPane.valignment="CENTER">
            <GridPane.margin>
                <Insets/>
            </GridPane.margin>
            <children>
                <Text stroke="BLACK" strokeType="OUTSIDE" strokeWidth="0.0"
                      text="Chosen file name" StackPane.alignment="CENTER_LEFT">
                    <StackPane.margin>
                        <Insets left="5.0"/>
                    </StackPane.margin>
                </Text>
            </children>
        </StackPane>
        <StackPane maxHeight="-Infinity" minHeight="-Infinity" prefHeight="25.0"
                   style="-fx-background-color: lightgrey;" GridPane.columnIndex="1"
                   GridPane.halignment="CENTER" GridPane.valignment="CENTER">
            <children>
                <Text strokeType="OUTSIDE" strokeWidth="0.0" text="x"/>
            </children>
        </StackPane>
        <StackPane maxHeight="-Infinity" minHeight="-Infinity" prefHeight="25.0"
                   style="-fx-background-color: lightgrey;" GridPane.columnIndex="2"
                   GridPane.halignment="CENTER" GridPane.valignment="CENTER">
            <children>
                <Text strokeType="OUTSIDE" strokeWidth="0.0" text="..."
                      StackPane.alignment="CENTER"/>
            </children>
        </StackPane>
    </children>
    <padding>
        <Insets bottom="5.0" left="5.0" right="5.0" top="5.0"/>
    </padding>
</GridPane>

当用户点击按钮(我使用Text作为按钮)时,它将显示文件选择器。选择文件后应显示文件名。一切正常,但是当文件名太长,文本溢出时会出现问题。

我希望文本以“...”结尾而不是溢出,并且大小不应该扩展其父级大小。

如何实现这一目标?

1 个答案:

答案 0 :(得分:3)

请改用Label。如果文本对于可用区域而言太长,则会自动使用省略号。与Text相比,它扩展了Region,因此可以通过父布局调整大小:

<?import javafx.scene.control.Label?>

...
<StackPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity"
           minWidth="-Infinity" prefHeight="25.0" prefWidth="200.0"
           style="-fx-background-color: lightgrey;" GridPane.valignment="CENTER">
    <GridPane.margin>
        <Insets/>
    </GridPane.margin>
    <children>
        <Label textFill="BLACK" text="Chosen file name" StackPane.alignment="CENTER_LEFT"
               ellipsisString="..." textOverrun="ELLIPSIS" wrapText="false">
            <StackPane.margin>
                <Insets left="5.0"/>
            </StackPane.margin>
        </Label>
    </children>
</StackPane>
...