BorderPane setCenter overflow

625 Views Asked by At

Please am having issues with my code, am trying to build a media player application in javaFx and am using BorderPane to layout the child nodes to top, center and bottom respectively, everything seems fine but when I set the center node the bottom node disappears.

I have a CSS file too but I don't think its part of the problem, unless am wrong.

Please, can anyone help out on how to layout the child nodes to top, center and button respectively.

Note: if I comment out

root.setCenter(new Main().mediaView(view));

line, the top and bottom nodes will be just fine.

Thanks.

Here's my code:

package application;
import java.io.File;

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.stage.StageStyle;


public class Main extends Application {

    @Override
    public void start(Stage primaryStage) {
        try {
            new Main().getPane(primaryStage);
        } catch(Exception e) {
            e.printStackTrace();
        }
    }


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

    public void getPane(Stage stage) {      

        Media media = new Media(new File("src/The_Nut_Job.mp4").toURI().toString());
        MediaPlayer mediaPlayer = new MediaPlayer(media);
        MediaView view = new MediaView(mediaPlayer);

        mediaPlayer.setOnReady(new Runnable() {
            @Override
            public void run() {

                double width = mediaPlayer.getMedia().getWidth();
                double height = mediaPlayer.getMedia().getHeight();

                System.out.println(width +", "+height);

                BorderPane root = new BorderPane();
                root.setTop(new Main().titleBar());
                root.setBottom(new Main().toolBar());

                //BorderPane.setAlignment(new Main().mediaView(view), Pos.CENTER);
                root.setCenter(new Main().mediaView(view));


                Scene scene = new Scene(root, width, height);
                scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
                stage.initStyle(StageStyle.TRANSPARENT);
                scene.setFill(Color.TRANSPARENT);
                stage.setScene(scene);
                stage.show();
                mediaPlayer.play();
            }
        });
    }

    public StackPane mediaView(MediaView mView) {
        StackPane view = new StackPane();
        view.getChildren().add(mView);
        view.setId("view");
        return view;
    }



    public HBox titleBar() {
        HBox titleBar = new HBox();
        titleBar.setMinHeight(35);
        titleBar.setMaxHeight(35);
        titleBar.setId("title");
        return titleBar;
    }

    public HBox toolBar() {
        HBox toolBar = new HBox();
        toolBar.setMinHeight(35);
        toolBar.setMaxHeight(35);
        toolBar.setId("tool");
        return toolBar;
    }
}

And here's my CSS:

/* JavaFX CSS - Leave this comment until you have at least create one rule which uses -fx-Property */
.root {
    -fx-border-color: #e6e6fa;
    -fx-border-radius: 8 8 0 0;
    -fx-border-width: 1;
    -fx-background-radius: 8 8 0 0;
    -fx-background-color: transparent;
}

#title {
    -fx-background-color: #222;
    -fx-border-color: transparent;
    -fx-border-radius: 8 8 0 0;
    -fx-background-radius: 8 8 0 0;
}

#tool {
    -fx-background-color: #222;
    -fx-border-color: white;
    -fx-border-radius: 0 0 8 8;
    -fx-background-radius: 0 0 8 8;
}

#view {
    -fx-background-color: transparent;
    -fx-border-radius: 0 0 8 8;
    -fx-background-radius: 0 0 8 8;
}
0

There are 0 best solutions below