javajavafxjavafx-8

Javafx textfield expanding to width of popup window


I'm using a JavaFX Popup window with a TextField inside, and I am trying to reduce the width of the TextField, but the Popup is preventing this and instead the TextField always grows to the width of the Popup.

public class InputPopup extends Popup {

  public InputPopup() {
    VBox vBox = new VBox();
    vBox.setPrefWidth(200);
    vBox.setPrefHeight(200);
    vBox.setPadding(new Insets(15));
    vBox.setSpacing(10);
    vBox.setAlignment(Pos.CENTER);

    Button closeBtn = new Button("Close");
    closeBtn.setOnAction(e -> this.hide());

    TextField textField = new TextField();
    textField.setMaxWidth(40.0);
    textField.setPrefWidth(40.0);
    textField.setMinWidth(40.0);

    vBox.getChildren().addAll(textField, closeBtn);
    getContent().add(vBox);
  }
}

I've tried using setPrefWidth, setPrefSize, setMinSize, setMaxSize. I've tried putting it inside a HBox. Nothing seems to work when it is inside a Popup window. What can I do to fix this?

EDIT

Turns out someone else in the team had pushed a css file that my code was reading from, there was a css problem that was overriding things, my code was correct but I didn't spot the css error, apologies for not spotting it, thank you for your help!


Solution

  • Try setting the width and height of the Popup also. The other option is to use Control.USE_PREF_SIZE. It should work.

    Key code

    this.setWidth(250);
    this.setHeight(250);
    

    or

    this.setWidth(Control.USE_PREF_SIZE);
    this.setHeight(Control.USE_PREF_SIZE);
    

    Full code

    Main

    import javafx.application.Application;
    import javafx.scene.control.Button;
    import javafx.scene.Scene;
    import javafx.scene.layout.StackPane;
    import javafx.stage.Stage;
    
    
    public class App extends Application
    {
        @Override
        public void start(Stage primaryStage)
        {
            InputPopup inputPopup = new InputPopup();
    
            Button button = new Button("Press me");
            button.setOnAction(actionEvent -> {
                if (!inputPopup.isShowing()) 
                         inputPopup.show(primaryStage); 
                     else
                         inputPopup.hide(); 
            });
    
           Scene scene = new Scene(new StackPane(button), 400, 400);
           primaryStage.setTitle("Rocket");
           primaryStage.setScene(scene);
           primaryStage.show(); 
        }
    }
    

    InputPopup

    import javafx.geometry.Insets;
    import javafx.geometry.Pos;
    import javafx.scene.control.Button;
    import javafx.scene.control.TextField;
    import javafx.scene.layout.VBox;
    import javafx.stage.Popup;
    
    public class InputPopup extends Popup {
    
      public InputPopup() {
        VBox vBox = new VBox();
        vBox.setPrefWidth(250);
        vBox.setPrefHeight(250);
        vBox.setPadding(new Insets(15));
        vBox.setSpacing(10);
        vBox.setAlignment(Pos.CENTER);
    
        Button closeBtn = new Button("Close");
        closeBtn.setOnAction(e -> this.hide());
    
        TextField textField = new TextField();
        textField.setMaxWidth(40.0);
        textField.setPrefWidth(40.0);
        textField.setMinWidth(40.0);
    
        vBox.getChildren().addAll(textField, closeBtn);
        getContent().add(vBox);
        this.setWidth(250);
        this.setHeight(250);
        vBox.setStyle("-fx-background-color: yellow");
      }
    }
    

    output

    enter image description here