JavaFX Stage/Scene automatic resize

后端 未结 2 633
死守一世寂寞
死守一世寂寞 2021-01-13 08:25

Like the picture showed, the red box above is a GridBox and below is a VBox with Splitpane (ListView) and Gridpane (2 Buttons). What I want to implement is to hide

相关标签:
2条回答
  • 2021-01-13 09:13

    I don't have enough reputation to make a comment (need 50, but I have 48) so I will make an answer. Now back on topic, try calling the method sizeToScene on your stage object after you remove the node.

    https://docs.oracle.com/javase/8/javafx/api/javafx/stage/Window.html#sizeToScene--

    0 讨论(0)
  • 2021-01-13 09:18

    I think Dusko is right given your approach, but I am asking why recreate the wheel? In this example, I use a TitledPane and adjust the Stage as the Scene's height gets larger or smaller. Thanks, @James_D for info on how to implement this.

    import javafx.application.Application;
    import javafx.application.Platform;
    import javafx.scene.Scene;
    import javafx.scene.control.Accordion;
    import javafx.scene.control.Button;
    import javafx.scene.control.Label;
    import javafx.scene.control.TextArea;
    import javafx.scene.control.TextField;
    import javafx.scene.control.TitledPane;
    import javafx.scene.layout.HBox;
    import javafx.scene.layout.VBox;
    import javafx.stage.Stage;
    
    /**
     *
     * @author blj0011
     */
    public class JavaFXApplication97 extends Application
    {
    
        @Override
        public void start(Stage primaryStage)
        {
            Label tpLabel = new Label("text here!");
            TextField tpTextField = new TextField();
            Button tpButton = new Button("OK");
            HBox tpHBox = new HBox(tpTextField, tpButton);
            VBox topPanel = new VBox(tpLabel, tpHBox);
    
            TextArea bpTextArea = new TextArea();
            Button bpButton = new Button("Button");
            VBox bpVBox = new VBox(bpTextArea, bpButton);
            TextArea bpTextArea2 = new TextArea();
            Button bpButton2 = new Button("Button2");
            VBox bpVBox2 = new VBox(bpTextArea2, bpButton2);
            HBox bpHBox = new HBox(bpVBox, bpVBox2);
    
            TitledPane titledPane = new TitledPane();
            titledPane.setText("");
            titledPane.setContent(bpHBox);
            titledPane.setAnimated(false);
            Accordion bottomPanel = new Accordion(titledPane);
            VBox root = new VBox(topPanel, bottomPanel);
    
            //This adjust the Stage when the height of the Accordian change.
            bottomPanel.expandedPaneProperty().addListener((obs, oldVal, newVal) -> {
                System.out.println("hello");
                Platform.runLater(() -> {
                    primaryStage.sizeToScene();    
                });
            });
    
            Scene scene = new Scene(root);
    
            primaryStage.setTitle("Hello World!");
            primaryStage.setScene(scene);
            primaryStage.show();
        }
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args)
        {
            launch(args);
        }
    
    }
    

    0 讨论(0)
提交回复
热议问题