Peter Penzov
Peter Penzov

Reputation: 1588

Button for changing size

I'm working on this code which is JavaFX panel with minimize button. I want then I press the button to shrink the size of the panel and again when I press the button to restore the original size.

public class MainApp extends Application
{

    private static BorderPane bp;

    @Override
    public void start(Stage stage) throws Exception
    {

        FlowPane flow = new FlowPane();
        flow.setPadding(new Insets(50, 5, 5, 5));
        flow.setVgap(15);
        flow.setHgap(15);
        flow.setAlignment(Pos.CENTER);

        Button button = new Button("Minimize");

        HBox thb = new HBox();
        thb.getChildren().add(button);
        thb.setAlignment(Pos.CENTER_RIGHT);

        boolean flag = true;


        button.setOnAction(new EventHandler<ActionEvent>()
        {

            @Override
            public void handle(ActionEvent t)
            {
                if (flag == true)
                {
                    flag = false;
                    bp.setPrefSize(600, 40);
                    bp.setMaxSize(600, 40);

                }
                else
                {
                    bp.setPrefSize(600, 500);
                    bp.setMaxSize(600, 500);
                }

            }
        });






        thb.setPadding(new Insets(13, 13, 13, 13));
        thb.setStyle("-fx-background-color: gray");

        DropShadow ds = new DropShadow();
        ds.setOffsetY(3.0);
        ds.setOffsetX(3.0);
        ds.setColor(Color.GRAY);

        bp = new BorderPane();
        bp.setEffect(ds);
        bp.setPrefSize(600, 500);
        bp.setMaxSize(600, 500);

        bp.setStyle("-fx-background-color:  white;");
        bp.setTop(thb);
        flow.getChildren().add(bp);
        Scene scene = new Scene(flow, 1200, 800);
        scene.getStylesheets().add("/styles/Styles.css");

        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args)
    {
        launch(args);
    }

}

I used Boolean flag bit for some reason the logic of the code is incorrect. Can you help me to fix this issue?

Upvotes: 0

Views: 60

Answers (1)

Anshul Parashar
Anshul Parashar

Reputation: 3126

use flag value true at else part

            if (flag == true)
            {
                flag = false;
                bp.setPrefSize(600, 40);
                bp.setMaxSize(600, 40);

            }
            else
            {
                flag = true;
                bp.setPrefSize(600, 500);
                bp.setMaxSize(600, 500);
            }

Upvotes: 1

Related Questions