admin管理员组

文章数量:1321048

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);
    }
}

本文标签: javaHow to make Labels fully visible in a GridPaneStack Overflow