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!
Try setting the width and height of the
Popupalso. The other option is to useControl.USE_PREF_SIZE. It should work.Key code
or
Full code
Main
InputPopup
output