This discussion is archived
2 Replies Latest reply: Jan 21, 2013 6:54 AM by KnutArneVedaa RSS

Simple layout question, shrinking TextField and TextArea inside VBox

KnutArneVedaa Newbie
Currently Being Moderated
Given:
        VBox root = new VBox(10);
        
        root.getChildren().addAll(new TextField(), new TextArea());
        
        Scene scene = new Scene(root, 300, 250);
When I shrink the application window vertically, the textfield starts to shrink first, while the text area stays its size, then when the textfield is only a narrow strip, the text area starts to shrink.

1) Is this expected?
2) Is it by design? If so, why?
3) How can I avoid it, i.e. have the text area shrink first, then the textfield?

It think the behaviour in 3) makes more sense, since a text area by its nature can have different heights; a text field on the other hand I would expect to have the height of a single line of text unless there is absolutely no space for it in its container. It doesn't make sense to have a textfield that is a fraction of the height of a line of text.

I have tried various combinations of minHeight and prefHeight, but without luck.
  • 1. Re: Simple layout question, shrinking TextField and TextArea inside VBox
    984080 Explorer
    Currently Being Moderated
    Hey,

    1) Yes, this is expected!
    2) Yes, why.. well I don't know why the minimum height of a TextField is smaller than the pref size.
    3) This can be avoid by setting your own sizes (min size - pref size - max size)
            VBox root = new VBox(10);
            root.setStyle("-fx-background-color: lightgray");
            
            TextField tf = new TextField();
            tf.setMinHeight(Control.USE_PREF_SIZE); //the minimum height = the preferred height
            
            TextArea ta = new TextArea();
            ta.setMinHeight(30);
            ta.setPrefHeight(50);
            ta.setMaxHeight(Double.MAX_VALUE); //maximum height, depends on the parent
    
            VBox.setVgrow(ta, Priority.ALWAYS); //use the maximum height available
            
            root.getChildren().addAll(tf, ta);
            
            Scene scene = new Scene(root, 300, 250);
    Edited by: FXdude on 21-jan-2013 6:13
  • 2. Re: Simple layout question, shrinking TextField and TextArea inside VBox
    KnutArneVedaa Newbie
    Currently Being Moderated
    Thanks.

    It seems that this:
            tf.setMinHeight(Control.USE_PREF_SIZE);
    is sufficient for the TextField to behave properly.

    I would expect that to be identical to:
            tf.setMinHeight(tf.getPrefHeight());
    but the latter doesn't work.

    What is the meaning of USE_PREF_SIZE? The documentation describes it as:
    Sentinel value which can be passed to a control's setMinWidth(), setMinHeight(), setMaxWidth() or setMaxHeight() methods to indicate that the preferred dimension should be used for that max and/or min constraint.
    What's a "sentinel value"?

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points