Search code examples
javafxheightfxml

Dynamically changing height with prefHeight is not working


This is my start method:

    Parent root = FXMLLoader.load(getClass().getResource("login.fxml"));

    primaryStage.initStyle(StageStyle.TRANSPARENT);
    Scene scene1 = new Scene(root);
    scene1.setFill(Color.TRANSPARENT);
    primaryStage.initStyle(StageStyle.TRANSPARENT);
    primaryStage.setScene(scene1);
    primaryStage.setTitle("Menu");
    primaryStage.show();

this is my FXML:

<Pane fx:id="loginPane"  maxHeight="-Infinity" maxWidth="- 
 Infinity"minHeight="-Infinity" minWidth="-Infinity" prefHeight="300.0" 
 prefWidth="600.0" xmlns="http://javafx.com/javafx/8" 
 xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">

<children>
<Button fx:id="login" layoutX="250.0" layoutY="37.0" 
 mnemonicParsing="false" onMouseClicked="#clickManager" text="login" />
</children>
 </Pane>

And my controller:

public class Controller implements Initializable {
    @FXML
    public Button login;
    @FXML
    public Pane loginPane;

    @FXML
    public void clickManager() {
        login.setOpacity(0);
        loginPane.setPrefHeight(700);
        loginPane.setStyle("-fx-background-color: black");
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {

    }
}

Chaning the color is working, but no matter how I play with maxheight and prefheight in fxml and controller It's just not working.Any ideas? What I mean is dinamically changing the height from the controller's method click manager when the button is clicked.


Solution

  • You can change the height by grabbing the window and setting it from there this is what your clickManager() should look like

    public void clickManager() {
        login.setOpacity(0);
        //loginPane.setPrefHeight(800);
        loginPane.getScene().getWindow().setHeight(700);
        loginPane.setStyle("-fx-background-color: black");
    }
    

    ̶I̶ ̶w̶o̶u̶l̶d̶ ̶a̶l̶s̶o̶ ̶r̶e̶m̶o̶v̶e̶ ̶t̶h̶e̶ ̶-̶I̶n̶f̶i̶n̶i̶t̶y̶'̶s̶ ̶e̶x̶.̶̶m̶i̶n̶W̶i̶d̶t̶h̶=̶"̶-̶I̶n̶f̶i̶n̶i̶t̶y̶"̶̶ ̶y̶o̶u̶ ̶h̶a̶v̶e̶ ̶i̶n̶ ̶y̶o̶u̶r̶ ̶f̶x̶m̶l̶ ̶s̶o̶ ̶i̶t̶ ̶d̶o̶e̶s̶n̶t̶ ̶a̶c̶c̶i̶d̶e̶n̶t̶a̶l̶l̶y̶ ̶m̶e̶s̶s̶ ̶w̶i̶t̶h̶ ̶a̶n̶y̶t̶h̶i̶n̶g̶

    Also you don't need to set this primaryStage.initStyle(StageStyle.TRANSPARENT); twice in your Start method