JavaFX: setHgrow (...) does not work

I could not get the elements in my HBox to grow, so I downloaded the following code sample from java2s.com . It serves as a minimal non-working example:

package fxtest;

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class Fxtest extends Application {
    public static void main(String[] args) {
        Application.launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("");
        Group root = new Group();
        Scene scene = new Scene(root, 600, 250, Color.WHITE);

        HBox hbox = new HBox();
        TextField field = new TextField();
        HBox.setHgrow(field, Priority.ALWAYS);
        hbox.getChildren().addAll(new Label("Search:"), field, new Button("Go"));


        root.getChildren().add(hbox);
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

The result looks like this .

If I understand correctly, TextField should grow, right?

My Java version is 1.7.0_51

My version of JavaFX is 2.2.51-b13

Do you know why it does not work / what should I do? Thanks!

+4
source share
2 answers

Why HBox.setHgrow Does n't Work For You

Group, node , .

.

""

, node .

(Pane Control ).

, node HBox node :

public void start(Stage primaryStage) {
    HBox hbox = new HBox(10);
    TextField field = new TextField();
    HBox.setHgrow(field, Priority.ALWAYS);
    hbox.setAlignment(Pos.BASELINE_LEFT);
    hbox.getChildren().addAll(
        new Label("Search:"), field, new Button("Go")
    );
    hbox.setPadding(new Insets(10));

    Scene scene = new Scene(hbox, 600, 250, Color.WHITE);
    primaryStage.setScene(scene);
    primaryStage.show();
}

( , , ):

grow

shrink

+7

jewelsea ; !

, . ScrollPanes,

    scrollPane.setFitToHeight(true);
    scrollPane.setFitToWidth(true);

.

+2

Source: https://habr.com/ru/post/1527587/


All Articles