JavaFX - MediaView getFitHeight() Method



In JavaFX, the getFitHeight() method in the MediaView class is used to retrieve the height of the bounding box of the resized media. By default, this method returns 0.0 if the value of 'fitHeight' is not set.

To retrieve the value of 'FitHeight' for the media, we should set the 'fitHeightProperty' by using the 'setFitHeight()' method. This allows us to specify the desired height of the media within the 'MediaView' bounding box. By setting the 'fitHeight' property, we ensure that the media is displayed at the intended height, and later on, we can retrieve this value using the 'getFitHeight()' method.

Syntax

Following is the syntax of the 'getFitHeight()' method of 'MediaView' class −

public final double getFitHeight()

Parameters

This method doesn't takes any parameters.

Return value

This method returns a double value that represents the height of the bounding box.

Example 1

Following is a basic example demonstrating the getFitHeight() method of 'MediaView' class −

In this example, we create an application that displays the video on the VBox. Then we displays the label for the fitHeight using the 'getFitHeight()' method.

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.Group;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import java.io.File;

public class FitHeight extends Application {
   @Override
   public void start(Stage stage) {
      File mediaPath = new File("./audio_video/sampleTP.mp4");
      Media media = new Media(mediaPath.toURI().toString());
      MediaPlayer mediaPlayer = new MediaPlayer(media);
      
      // Create the MediaView and set fit height
      MediaView mediaView = new MediaView(mediaPlayer);
      mediaView.setFitHeight(240);

      // Create a VBox to hold the label and MediaView
      VBox root = new VBox();
      Label fitHeightLabel = new Label("Fit height of the media: " + mediaView.getFitHeight());
      
      root.getChildren().addAll(mediaView, fitHeightLabel);    

      Scene scene = new Scene(root, 550, 280);
      stage.setScene(scene);
      stage.setTitle("Fit Height Example");
      stage.show();
      
      mediaPlayer.play();
   }   
   public static void main(String[] args) {
      launch(args);
   }
}

Output

Following is the output of the code −

getHeight1

Example 2

In this example, we use the 'getFitHeight()' method to retrieve the value of the 'fitHeight' without setting it.

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.Group;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import java.io.File;
public class FitHeight1 extends Application {
   @Override
   public void start(Stage stage) {
      File mediaPath = new File("./audio_video/sampleTP.mp4");
      Media media = new Media(mediaPath.toURI().toString());
      MediaPlayer mediaPlayer = new MediaPlayer(media);
      
      // Create the MediaView and set fit height
      MediaView mediaView = new MediaView(mediaPlayer);

      // Create a VBox to hold the label and MediaView
      VBox root = new VBox();
      double fitheight = mediaView.getFitHeight();
      System.out.println("Fit height of the media: " + fitheight);
      
      root.getChildren().add(mediaView);    

      Scene scene = new Scene(root, 550, 280);
      stage.setScene(scene);
      stage.setTitle("Fit Height Example");
      stage.show();
      
      mediaPlayer.play();
   } 
   public static void main(String[] args) {
      launch(args);
   }
}

Output

Following is the output of the code, which displays the 'fitHeight' on the console.

Fit height of the media: 0.0
Advertisements