4 Replies Latest reply on Nov 2, 2013 6:23 PM by leifs

    TextField prefWidth (not PrefColumnCount)

    Jeff Martin

      How do I size a TextField to its prefWidth, not its getPrefColumnCount. Is there a special value of PrefColumnCount that tells TextField to use its prefWidth?

        • 1. Re: TextField prefWidth (not PrefColumnCount)
          leifs

          The default for TextField  is to calculate prefWidth based on prefColumnCount. Are you thinking about calculating it based on the actual width of the string? If, so you'd have to do that yourself and call setPrefWidth().

          • 2. Re: TextField prefWidth (not PrefColumnCount)
            Jeff Martin

            I thought I might be able to do that by calling TextField.setPrefWidth() with: new Label("MyText").prefWidth(-1), but that returns 0. Any thoughts on the best solution?

            • 3. Re: TextField prefWidth (not PrefColumnCount)
              James_D

              Here's a solution based on a lookup. I consider this a bit of a hack...

               

              import javafx.application.Application;
              import javafx.beans.value.ChangeListener;
              import javafx.beans.value.ObservableValue;
              import javafx.geometry.Bounds;
              import javafx.geometry.Insets;
              import javafx.scene.Node;
              import javafx.scene.Scene;
              import javafx.scene.control.TextField;
              import javafx.scene.layout.HBox;
              import javafx.stage.Stage;
              
              public class TextFieldWidthTest extends Application {
              
                  @Override
                  public void start(Stage primaryStage) {
                      final HBox root = new HBox();
                      root.setPadding(new Insets(5));
                      final TextField textField = new TextField();
                      root.getChildren().add(textField);
                      primaryStage.setScene(new Scene(root, 200, 50));
                      primaryStage.show();
              
                      final Node text = textField.lookup(".text");
                      textField.setPrefWidth(text.getBoundsInLocal().getWidth() + 10);
                      text.boundsInLocalProperty().addListener(new ChangeListener<Bounds>() {
                          @Override
                          public void changed(ObservableValue<? extends Bounds> obs,
                                  Bounds oldBounds, Bounds newBounds) {
                              textField.setPrefWidth(newBounds.getWidth() + 10);
                          }
                      });
                  }
              
                  public static void main(String[] args) {
                      launch(args);
                  }
              }
              
              • 4. Re: TextField prefWidth (not PrefColumnCount)
                leifs

                JeffMartin wrote:

                 

                I thought I might be able to do that by calling TextField.setPrefWidth() with: new Label("MyText").prefWidth(-1), but that returns 0. Any thoughts on the best solution?

                It returns zero because Label is a Control and it doesn't have a skin yet, and CSS hasn't been processed. You can try wrapping the statement in a call to Platform.runLater().

                 

                If the Label is created only temporarily to be used for measuring, then use a Text node instead. It is not a Control and should have its prefWidth ready right away.