最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - How to make Labels fully visible in a GridPane? - Stack Overflow

programmeradmin7浏览0评论

I have a GridPane where labels and textfields alternate (label, textfield, label, textfield). The labels can be of any width and it is never known in advance. I need to ensure that the labels are always fully visible, meaning they are not truncated (...), and only the TextFields shrink. This is my code:

public class NewMain extends Application {

    @Override
    public void start(Stage stage) {
        GridPane gridPane = new GridPane();
        gridPane.setHgap(10);
        gridPane.setVgap(10);
        gridPane.setPadding(new Insets(10));

        Label label1 = new Label("Label AAAAA BBBBB:");
        label1.setMinWidth(Region.USE_COMPUTED_SIZE);
        TextField textField1 = new TextField();
        textField1.setMinWidth(10);
        GridPane.setHgrow(textField1, Priority.ALWAYS);
        Label label2 = new Label("Label CCCCCC DDDDDD:");
        label2.setMinWidth(Region.USE_COMPUTED_SIZE);
        TextField textField2 = new TextField();
        GridPane.setHgrow(textField2, Priority.ALWAYS);
        textField2.setMinWidth(10);

        gridPane.add(label1, 0, 0);
        gridPane.add(textField1, 1, 0);
        gridPane.add(label2, 2, 0);
        gridPane.add(textField2, 3, 0);

        ColumnConstraints labelColumn = new ColumnConstraints();
        labelColumn.setHgrow(Priority.NEVER);

        ColumnConstraints textFieldColumn = new ColumnConstraints();
        textFieldColumn.setMinWidth(50);
        textFieldColumn.setHgrow(Priority.ALWAYS);

        gridPane.getColumnConstraints().addAll(
            labelColumn, textFieldColumn,
            labelColumn, textFieldColumn
        );

        Scene scene = new Scene(gridPane, 600, 100);
        stage.setTitle("GridPane Single Row Test");
        stage.setScene(scene);
        stage.show();
    }


    public static void main(String[] args) {
        launch(args);
    }
}

And this is the result:

Could anyone say how to do it?

I have a GridPane where labels and textfields alternate (label, textfield, label, textfield). The labels can be of any width and it is never known in advance. I need to ensure that the labels are always fully visible, meaning they are not truncated (...), and only the TextFields shrink. This is my code:

public class NewMain extends Application {

    @Override
    public void start(Stage stage) {
        GridPane gridPane = new GridPane();
        gridPane.setHgap(10);
        gridPane.setVgap(10);
        gridPane.setPadding(new Insets(10));

        Label label1 = new Label("Label AAAAA BBBBB:");
        label1.setMinWidth(Region.USE_COMPUTED_SIZE);
        TextField textField1 = new TextField();
        textField1.setMinWidth(10);
        GridPane.setHgrow(textField1, Priority.ALWAYS);
        Label label2 = new Label("Label CCCCCC DDDDDD:");
        label2.setMinWidth(Region.USE_COMPUTED_SIZE);
        TextField textField2 = new TextField();
        GridPane.setHgrow(textField2, Priority.ALWAYS);
        textField2.setMinWidth(10);

        gridPane.add(label1, 0, 0);
        gridPane.add(textField1, 1, 0);
        gridPane.add(label2, 2, 0);
        gridPane.add(textField2, 3, 0);

        ColumnConstraints labelColumn = new ColumnConstraints();
        labelColumn.setHgrow(Priority.NEVER);

        ColumnConstraints textFieldColumn = new ColumnConstraints();
        textFieldColumn.setMinWidth(50);
        textFieldColumn.setHgrow(Priority.ALWAYS);

        gridPane.getColumnConstraints().addAll(
            labelColumn, textFieldColumn,
            labelColumn, textFieldColumn
        );

        Scene scene = new Scene(gridPane, 600, 100);
        stage.setTitle("GridPane Single Row Test");
        stage.setScene(scene);
        stage.show();
    }


    public static void main(String[] args) {
        launch(args);
    }
}

And this is the result:

Could anyone say how to do it?

Share Improve this question asked Jan 17 at 19:38 SilverCubeSilverCube 6962 silver badges12 bronze badges 0
Add a comment  | 

1 Answer 1

Reset to default 5

The computed size for a label takes into account the fact that it can be truncated. The preferred size will give you the size it "wants to be", and it would prefer not to be truncated. Just set the minimum width to Region.USE_PREF_SIZE:

public class NewMain extends Application {

    @Override
    public void start(Stage stage) {
        GridPane gridPane = new GridPane();
        gridPane.setHgap(10);
        gridPane.setVgap(10);
        gridPane.setPadding(new Insets(10));

        Label label1 = new Label("Label AAAAA BBBBB:");
        label1.setMinWidth(Region.USE_PREF_SIZE);
        TextField textField1 = new TextField();
        textField1.setMinWidth(10);
        Label label2 = new Label("Label CCCCCC DDDDDD:");
        label2.setMinWidth(Region.USE_PREF_SIZE);
        TextField textField2 = new TextField();
        textField2.setMinWidth(10);

        gridPane.add(label1, 0, 0);
        gridPane.add(textField1, 1, 0);
        gridPane.add(label2, 2, 0);
        gridPane.add(textField2, 3, 0);


        Scene scene = new Scene(gridPane, 600, 100);
        stage.setTitle("GridPane Single Row Test");
        stage.setScene(scene);
        stage.show();
    }


    public static void main(String[] args) {
        launch(args);
    }
}
发布评论

评论列表(0)

  1. 暂无评论