Abdullah Shahid
Abdullah Shahid

Reputation: 111

How to bind dimensions of any component to the parent container in JavaJX?

I am creating an application in which i need to play video as shown below.

Screenshot of my App

The red boundary represents an AnchorPane. There is MediaView in AnchorPane.

All i want is to make the MediaView (or the video) grow and shrink as i move the divider of split Pane. The code which i have written makes the video grow but when i move the divider of split pane back, the video does not shrink. Rather the video frame is cropped from the sides.

How to bind the dimensions of mediaView to its parent component? OR Whats wrong with my code ?

following is my code ...

public void start(Stage primaryStage) {

        SplitPane video_text_SplitPane = new SplitPane();

        Label label2 = new Label("abdsanjasj");
        AnchorPane video_AnchorPane = new AnchorPane();

        video_AnchorPane.setBorder(new Border(new BorderStroke(Color.RED,
                BorderStrokeStyle.SOLID, CornerRadii.EMPTY, BorderWidths.DEFAULT)));
        AnchorPane anchorPane2 = new AnchorPane();

        AnchorPane.setBottomAnchor(label2, 0.0);
        AnchorPane.setTopAnchor(label2, 0.0);
        AnchorPane.setLeftAnchor(label2, 0.0);
        AnchorPane.setRightAnchor(label2, 0.0);

        anchorPane2.getChildren().add(label2);

//                  ---- CODE FOR PLAYING VIDEO ---
        MediaView mediaView = new MediaView();

        Media media = new Media("file:/E:/video.mp4");
        MediaPlayer mediaPlayer = new MediaPlayer(media);
        mediaView.setMediaPlayer(mediaPlayer);

        mediaPlayer.setAutoPlay(true);


//        mediaView.autosize();
//        DoubleProperty width = mediaView.fitWidthProperty();
//        DoubleProperty height = mediaView.fitHeightProperty();
//        width.bind(Bindings.selectDouble(mediaView.sceneProperty(), "width"));
//        height.bind(Bindings.selectDouble(mediaView.sceneProperty(), "height"));

        video_AnchorPane.getChildren().add(mediaView);
        video_text_SplitPane.getItems().add(video_AnchorPane);
        AnchorPane.setBottomAnchor(video_AnchorPane, 0.0);
        AnchorPane.setTopAnchor(video_AnchorPane, 0.0);
        AnchorPane.setLeftAnchor(video_AnchorPane, 0.0);
        AnchorPane.setRightAnchor(video_AnchorPane, 0.0);
        mediaView.setPreserveRatio(true);
        video_text_SplitPane.getItems().add(anchorPane2);


//      ---- CODE FOR BINDING MEDIA VIEW DIMENSIONS TO ITS PARENT CONTAINER---

        mediaView.fitWidthProperty().bind(video_AnchorPane.widthProperty());
        mediaView.fitHeightProperty().bind(video_AnchorPane.heightProperty());


        BorderPane video_textArea = new BorderPane();
        video_textArea.setTop(rangeSliderAnchorPane);
        video_textArea.setBottom(videoControlAnchorPane);
        video_textArea.setCenter(video_text_SplitPane);=

        SplitPane splitPane = new SplitPane();
        splitPane.setOrientation(Orientation.HORIZONTAL);
        splitPane.getItems().add(tree);

        splitPane.getItems().add(video_textArea);

        BorderPane layout = new BorderPane();
        layout.setTop(menuBar);
        layout.setCenter(splitPane);
        layout.setBottom(new StatusBar());

        Scene scene = new Scene(layout, 1920, 990);

        primaryStage.setTitle("Subtitles Generator");
        primaryStage.setScene(scene);
        primaryStage.show();

    }

Upvotes: 0

Views: 812

Answers (1)

c0der
c0der

Reputation: 18802

Is this the what you are trying to achieve ?

enter image description here

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.SplitPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.StackPane;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.stage.Stage;
import javafx.util.Duration;

public class Player extends Application {

    @Override
    public void start(Stage primaryStage) {

        Label label2 = new Label("Lorem ipsum dolor sit amet, consectetur adipiscing elit\n, "
                               + "sed do eiusmod tempor incididunt ut labore et dolore magna\n"
                               + "aliqua. Ut enim ad minim veniam, quis nostrud exercitation \n"
                               + "ullamco laboris nisi ut aliquip ex ea commodo consequat");
        StackPane textPane = new StackPane();
        textPane.getChildren().add(label2);

        MediaView mediaView = new MediaView();
        Media media = new Media("http://techslides.com/demos/sample-videos/small.mp4");
        MediaPlayer mediaPlayer = new MediaPlayer(media);
        mediaView.setMediaPlayer(mediaPlayer);
        mediaPlayer.setAutoPlay(true);
        mediaPlayer.setOnEndOfMedia (() -> {
            mediaPlayer.seek(Duration.ZERO);
            mediaPlayer.play();
        });
        mediaView.setPreserveRatio(true);
        mediaView.autosize();

        BorderPane videoPane = new BorderPane();
        videoPane.getChildren().add(mediaView);

        SplitPane videoTextSplitPane = new SplitPane();
        videoTextSplitPane.getItems().add(videoPane);
        videoTextSplitPane.getItems().add(textPane);

        mediaView.fitWidthProperty().bind(videoPane.widthProperty());
        Scene scene = new Scene(videoTextSplitPane, 800, 400);

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

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

Upvotes: 2

Related Questions