Reputation: 358
I'm currently working on an app that will play a video using JavaFX. I want to place the volume slider on the right side of the border pane. Currently, the app will resize to the size of the window, which will overlap with my volume slider.
I have a controller that takes care of all the actions performed by the app and implements the Initializable interface. Here's how my initialize() method looks like.
@Override
public void initialize(URL locate, ResourceBundle resources){
String path = new File("src/media/dance.mp4").getAbsolutePath();
media = new Media (new File (path).toURI().toString());
player = new MediaPlayer(media);
mediaView.setMediaPlayer(player);
DoubleProperty width = mediaView.fitWidthProperty();
DoubleProperty height = mediaView.fitHeightProperty();
width.bind(Bindings.selectDouble(mediaView.sceneProperty(), "width"));
height.bind(Bindings.selectDouble(mediaView.sceneProperty(), "height"));
volumeSlider.setValue(player.getVolume() * 100);
volumeSlider.valueProperty().addListener(new InvalidationListener() {
@Override
public void invalidated(Observable observable) {
player.setVolume(volumeSlider.getValue() / 100);
}
});
}
I tried to subtract 64px directly after mediaView.fitWidthProperty()
and mediaView.sceneProperty()
, but both gave me an error because they are not of type double. I tried mediaView.fitWidthProperty().subtract(64)
, but it said I would need to change the data type from DoubleProperty to DoubleBind, which would not be compatible with Bindings.selectDouble()
method that requires a DoubleProperty.
How can I get this to work?
Upvotes: 1
Views: 1795
Reputation: 34528
EDITED
There is an easier way! Just bind to the property directly:
mediaView.fitWidthProperty().bind(
mediaView.getScene().widthProperty().subtract(64));
Here is a full app to demonstrate:
public class FXWidthBind extends Application {
@Override
public void start(Stage stage) {
Media media = new Media("http://i.imgur.com/OJTwZuc.mp4");
MediaPlayer player = new MediaPlayer(media);
MediaView mediaView = new MediaView(player);
Pane root = new Pane();
Scene scene = new Scene(root);
root.getChildren().add(mediaView);
stage.setWidth(1000);
stage.setHeight(800);
stage.setScene(scene);
stage.show();
mediaView.fitWidthProperty().bind(mediaView.getScene().widthProperty().subtract(64));
mediaView.fitHeightProperty().bind(mediaView.getScene().heightProperty());
}
public static void main(String[] args) {
launch(args);
}
}
Upvotes: 1
Reputation: 358
The solution is to subtract the width property of your SceneProperty (video screen).
width.bind(Bindings.selectDouble(mediaView.sceneProperty(), "width").subtract(64.0));
Here, I'm binding the width of my MediaView with the width of my SceneProperty minus 64 pixels. Since DoubleProperty is a wrapper around the primitive type double, you can use the subtract()
method to modify the actual value underneath.
Before
After
Upvotes: 0