JavaFX标签高度

时间:2014-01-08 03:38:12

标签: java height javafx label scenebuilder

我正在使用JavaFX和JavaFX Scene Builder开发软件。

我有一个包含2列的网格窗格。实际上在每个单元格中都有标签。 在表格的第一列中,标签内的文本是默认常量,在第二列中文本可以更改。

问题是如果文本变量太长,它会自动截断。

如何开始修剪文本的新行并重新调整网格窗格中行的高度?

修改

感谢@fabian问题,这是初始问题的解决方案。 在成瘾中,必须将每个元素的fx:id设置为Scene Builder。

@FXML private GridPane gridPane;
@FXML private Text text1;
@FXML private Text text2;
@FXML private Text text3;


private void setGridRowHeight(GridPane gpName, int numRow, double height){
    ObservableList<RowConstraints>  rows = gpName.getRowConstraints();
    rows.get(numRow).setMinHeight(32);
    rows.get(numRow).setPrefHeight(height);
    rows.get(numRow).setMaxHeight(height);
}


private void addTextListener(final GridPane gridPane, final Text text){

    text.textProperty().addListener(new ChangeListener<String>() {
        @Override
        public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
            scrollPane.setFitToHeight(true);
            text.setWrappingWidth( gridPane.getColumnConstraints().get(1).getPrefWidth() );
            setGridRowHeight(gridPane, gridPane.getRowIndex(text), text.getLayoutBounds().getHeight() );
        }
    });
}


@FXML public void initialize() {
    addTextListener(gridPane, text1);
    addTextListener(gridPane, text2);
    addTextListener(gridPane, text3);
}    

3 个答案:

答案 0 :(得分:6)

不幸的是,Label元素不会根据显示的文本调整大小。我建议使用Text形状。您可以在场景构建器中使用包装宽度指定最大宽度。

答案 1 :(得分:3)

你可以尝试:

Label col2Label = new Label();
col2Label.setWrapText( true );
col2Label.setMaxHeight( Double.MAX_VALUE );
col_1_Label.prefHeightProperty().bind( col2Label.heightProperty() );

答案 2 :(得分:0)

您可以在HBox中包装[Region - Label - Region]。 该区域将使标签始终居中。