title: How to set a fixed height for a JavaFX text field regardless of its contents?
body: <p>I'm making a notepad application using JavaFX which includes a font selector window. The font selector includes a textfield within a gridpane layout which functions as a sample of what the selected font options will look like.</p>
  
  <p>Font selector:<br>
  <a href="https://i.stack.imgur.com/L1iI2.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/L1iI2.png" alt="font selector"></a></p>
  
  <p>However, if the font chosen is taller than the text field, the field stretches to fit the text, distorting the window. </p>
  
  <p>Distorted font selector:<br>
  <a href="https://i.stack.imgur.com/7wGrE.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/7wGrE.png" alt="distorted font selector"></a></p>
  
  <p>I want the sample textfield to remain the same size even if the text contained is taller than the field, like the font selector shown below:<br>
  <a href="https://i.imgur.com/an0iINR.png" rel="nofollow noreferrer"><img src="https://i.imgur.com/an0iINR.png" alt="font selector"></a></p>
  
  <p>I've tried using the 'setPrefSize' and 'setMaxSize' textfield methods to force a maximum height.</p>
  
  <pre><code>public static final String display(String savedFamily, String savedStyle, String savedSize, String stylesheet) {
  
      fontFamily = savedFamily;
      fontSize = savedSize.substring(0, savedSize.length() - 2);
      fontWeight = getChosenWeight(savedStyle);
      fontStyle = getChosenStyle(savedStyle);
  
      // Add stage
      Stage window = new Stage();
      window.initModality(Modality.APPLICATION_MODAL);
  
      TextField sample = new TextField("AaBbYyZz");
      sample.setEditable(false);
      sample.getStyleClass().add("sample");
      sample.setAlignment(Pos.CENTER);
      sample.setStyle(getCSS());
      sample.setPrefSize(200, 60);
      sample.setMaxHeight(60);
  
      // Add list view title labels
      Label fontLabel = new Label("Font:");
      Label fontStyleLabel = new Label("Font Style:");
      Label fontSizeLabel = new Label("Font Size: ");
  
      // Add font list view
      ListView&lt;String&gt; fontView = new ListView&lt;&gt;();
      String fonts[] = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
      for (int i = 0; i &lt; fonts.length; i++) {
          fontView.getItems().add(fonts[i]);
      }
      fontView.getSelectionModel().select(fontFamily);
      fontView.setMaxSize(200, 150);
      fontView.getSelectionModel().selectedItemProperty().addListener((v) -&gt; {
          fontFamily = fontView.getSelectionModel().getSelectedItem();
          sample.setStyle(getCSS());
      });
  
      // Add font style list view
      ListView&lt;String&gt; fontStyleView = new ListView&lt;&gt;();
      fontStyleView.getItems().addAll("Regular", "Italic", "Bold", "Bold Italic");
      fontStyleView.getSelectionModel().select(savedStyle);
      fontStyleView.setMaxSize(80, 150);
      fontStyleView.getSelectionModel().selectedItemProperty().addListener((v) -&gt; {
          fontStyle = getChosenStyle(fontStyleView.getSelectionModel().getSelectedItem());
          fontWeight = getChosenWeight(fontStyleView.getSelectionModel().getSelectedItem());
          sample.setStyle(getCSS());
      });
  
      // Add font size list view
      ListView&lt;String&gt; fontSizeView = new ListView&lt;&gt;();
      fontSizeView.getItems().addAll("6", "7", "8", "9", "10", "11", "12", "14", "16", "18", "20", "22", "24", "26", "28");
      fontSizeView.getSelectionModel().select(fontSize);
      fontSizeView.setMaxSize(80, 150);
      fontSizeView.getSelectionModel().selectedItemProperty().addListener((v) -&gt; {
          fontSize = fontSizeView.getSelectionModel().getSelectedItem();
          sample.setStyle(getCSS());
      });
  
      // Add OK button
      Button okButton = new Button("OK");
      okButton.setOnAction(e -&gt; {
          fontCssString = getCSS();
          window.close();
      });
  
      // Add cancel button
      Button cancelButton = new Button("Cancel");
      cancelButton.setOnAction(e -&gt; {
          window.close();
      });
  
      // Add and configure grid pane
      GridPane layout = new GridPane();
      layout.setPadding(new Insets(15, 15, 15, 15));
      layout.setVgap(20);
      layout.setHgap(20);
  
      // Set grid constraints for font list view and title
      GridPane.setConstraints(fontLabel, 0, 0);
      GridPane.setConstraints(fontView, 0, 1);
  
      // Set grid constraints for font style list view and title
      GridPane.setConstraints(fontStyleLabel, 1, 0);
      GridPane.setConstraints(fontStyleView, 1, 1);
      GridPane.setValignment(fontStyleView, VPos.TOP);
  
      // Set grid constraints for font size list view and title
      GridPane.setConstraints(fontSizeLabel, 2, 0);
      GridPane.setConstraints(fontSizeView, 2, 1);
  
      // Set grid constraints for sample title
      GridPane.setConstraints(sample, 0, 2);
      GridPane.setHalignment(sample, HPos.CENTER);
  
      // set grid constraints and alignments for buttons
      GridPane.setConstraints(okButton, 1, 2);
      GridPane.setHalignment(okButton, HPos.RIGHT);
      GridPane.setConstraints(cancelButton, 2, 2);
  
      // Add items to grid pane layout
      layout.getChildren().addAll(fontLabel, fontView, fontStyleLabel, fontStyleView, fontSizeLabel, fontSizeView,
              sample, okButton, cancelButton);
  
      // configure the scene and stage
      Scene scene = new Scene(layout, 450, 300);
      scene.getStylesheets().add(FontWindow.class.getResource(stylesheet).toExternalForm());
      window.setScene(scene);
      window.setTitle("Font Options");
      window.showAndWait();
  
      return fontCssString;
  }
  
  public static final String getChosenStyle(String s) {
      String result = "normal";
      if (s.contains("Italic")) {
          result = "italic";
      }
      return result;
  }
  
  public static final String getChosenWeight(String s) {
      String result = "normal";
      if (s.contains("Bold")) {
          result = "bold";
      }
      return result;
  }
  
  public static String getCSS() {
      return "-fx-font-family: " + fontFamily + "; -fx-font-weight: " + fontWeight + "; -fx-font-style: " + fontStyle
              + "; -fx-font-size: " + Float.valueOf(fontSize) / 9.7 + "em;";
  }
  }
  </code></pre>
  
  <p>Edit: This has been solved! I was able to get the text field to function as I wanted by setting the maximum and minimum size as well as setting the preferred height and width to 'region.USE_COMPUTED_SIZE'.</p>