How do I play media in a Stack Pane in Javafx? - java

I'm learning how to use JavaFx and I am trying to create a media player with two scenes where one plays videos and the other plays music. I am working on the video scene right now and cannot figure out why my code isn't working.
public class MediaApp extends Application {
Media video_source;
MediaPlayer video_mp;
MediaView video_mv;
#Override
public void start(Stage primaryStage) {
Pane root1 = new Pane();
BorderPane videoPane = new BorderPane();
Scene vidScene = new Scene(videoPane, 1280, 800);
Scene mainScene = new Scene(root1, 1280, 800);
MenuBar video_menuBar = new MenuBar();
Menu video_menuFile = new Menu("File");
MenuItem video_open = new MenuItem("Open File...");
video_open.setOnAction((ActionEvent t) -> {
File file = new FileChooser().showOpenDialog(primaryStage);
if (file != null) {
video_source = new Media(file.toURI().toString());
video_mv = new MediaView(video_mp);
}
});
video_menuFile.getItems().addAll(video_open);
Menu video_menuEdit = new Menu("Edit");
Menu video_menuView = new Menu("View");
MenuItem mainPage = new MenuItem("Main Menu");
mainPage.setOnAction(e -> primaryStage.setScene(mainScene));
video_menuView.getItems().add(mainPage);
video_menuBar.getMenus().addAll(video_menuFile, video_menuEdit, video_menuView);
video_menuBar.prefWidthProperty().bind(videoPane.widthProperty());
videoPane.setTop(video_menuBar);
StackPane stack = new StackPane();
stack.getChildren().addAll(new Rectangle(1000, 600, Color.BLACK), video_mv);
videoPane.setCenter(stack);
video_mp.play();
primaryStage.setScene(mainScene);
primaryStage.setTitle("Media Player");
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
I would also like to note, that I want a black rectangle present even when there isn't a video playing.

There are actually several problems in your code. In short, in setOnAction you should create the MediaPlayer from the chosen video file, then set it as a player for MediaView and then play it.
Here is the working example:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
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.scene.shape.Rectangle;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import java.io.File;
public class Video extends Application {
#Override
public void start(Stage primaryStage) {
Pane root = new Pane();
BorderPane videoPane = new BorderPane();
Scene mainScene = new Scene(root, 1280, 800);
MenuBar video_menuBar = new MenuBar();
Menu video_menuFile = new Menu("File");
MediaView video_mv = new MediaView();
MenuItem video_open = new MenuItem("Open File...");
video_open.setOnAction(t -> {
FileChooser chooser = new FileChooser();
File file = chooser.showOpenDialog(primaryStage);
if (file != null) {
Media video_source = new Media(file.toURI().toString());
MediaPlayer video_mp = new MediaPlayer(video_source);
video_mv.setMediaPlayer(video_mp);
video_mp.play();
}
});
video_menuFile.getItems().addAll(video_open);
Menu video_menuEdit = new Menu("Edit");
Menu video_menuView = new Menu("View");
MenuItem mainPage = new MenuItem("Main Menu");
mainPage.setOnAction(e -> primaryStage.setScene(mainScene));
video_menuView.getItems().add(mainPage);
video_menuBar.getMenus().addAll(video_menuFile, video_menuEdit, video_menuView);
video_menuBar.prefWidthProperty().bind(videoPane.widthProperty());
videoPane.setTop(video_menuBar);
StackPane stack = new StackPane();
stack.getChildren().addAll(new Rectangle(1000, 600, Color.BLACK), video_mv);
videoPane.setCenter(stack);
root.getChildren().add(videoPane);
primaryStage.setScene(mainScene);
primaryStage.setTitle("Media Player");
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Note, that some video formats are not supported.

Related

Why is my Button Image setGraphic method not being displayed?

I have the image files in my bin folder but when I run my program all I have is my button options that are not being replaced with my images. I think it has something to do with the file: in front of the images but I'm not sure. The images are not actually a moving gif (dunno why its called gif).
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
public class Project4 extends Application {
public static void main(String[] args) {
launch(args);
}
public void start(Stage primaryStage) throws Exception {
HBox root = new HBox();
Button btnKings;
Button btnDucks;
Button btnSharks;
Button btnBlues;
btnKings = new Button("Kings");
btnDucks = new Button("Ducks");
btnSharks = new Button("Sharks");
btnBlues = new Button("Blues");
Image imgKings = new Image("file:kings.jpg");
btnKings.setGraphic(new ImageView(imgKings));
Image imgDucks = new Image("file:ducks.gif");
btnDucks.setGraphic(new ImageView(imgDucks));
Image imgSharks = new Image("file:sharks.gif");
btnSharks.setGraphic(new ImageView(imgSharks));
Image imgBlues = new Image("file:blues.gif");
btnBlues.setGraphic(new ImageView(imgBlues));
HandleButtonClick clickEvent = new HandleButtonClick();
btnKings.setOnAction(clickEvent);
btnDucks.setOnAction(new HandleButtonClick("You clicked the ducKS!"));
btnKings.setOnAction(new HandleButtonClick("You clicked the Kings"));
btnSharks.setOnAction(new HandleButtonClick("You clicked the sharks"));
btnBlues.setOnAction(new HandleButtonClick("You clicked the blues"));
root.getChildren().add(btnKings);
root.getChildren().add(btnDucks);
root.getChildren().add(btnSharks);
root.getChildren().add(btnBlues);
Scene scene = new Scene(root, 960, 130);
primaryStage.setTitle("HockeyButtons");
primaryStage.setScene(scene);
primaryStage.show();
}
}
I cant find out how to replace the words buttons with the pictures
Nghia Duong
If you want only images to be shown on the button but text then you should create Button without text.
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
public class Project4 extends Application {
public static void main(String[] args) {
launch(args);
}
public void start(Stage primaryStage) throws Exception {
HBox root = new HBox();
Button btnKings;
Button btnDucks;
Button btnSharks;
Button btnBlues;
btnKings = new Button();
btnDucks = new Button();
btnSharks = new Button();
btnBlues = new Button();
Image imgKings = new Image("kings.jpg");
btnKings.setGraphic(new ImageView(imgKings));
Image imgDucks = new Image("ducks.gif");
btnDucks.setGraphic(new ImageView(imgDucks));
Image imgSharks = new Image("sharks.gif");
btnSharks.setGraphic(new ImageView(imgSharks));
Image imgBlues = new Image("blues.gif");
btnBlues.setGraphic(new ImageView(imgBlues));
root.getChildren().add(btnKings);
root.getChildren().add(btnDucks);
root.getChildren().add(btnSharks);
root.getChildren().add(btnBlues);
Scene scene = new Scene(root, 960, 130);
primaryStage.setTitle("HockeyButtons");
primaryStage.setScene(scene);
primaryStage.show();
}
}
And here you are showing the path of the image incorrectly.
Image imgKings = new Image("file:kings.jpg");
You'd better show absolute path or if the images are in classpath folder then by:
String absolutePathToIcon =
getClass().getResource("kings.jpg").toExternalForm();
Image imgKings = new Image(absolutePathToIcon);

How to set menu bar with child window in javafx?

vbox2.setPadding(new Insets(3));
vbox2.setSpacing(3);
vbox2.getChildren().addAll( browser1,browser);
HBox.setHgrow(vbox2, Priority.ALWAYS);
hbox.setPadding(new Insets(20));
// StackPane.setMargin(hbox, new Insets(20));
hbox.getChildren().addAll(vbox, vbox2);
root.getChildren().add(hbox);
Scene scene = new Scene(root, 500, 300); // the stack pane is the root node
//scene.setCursor(Cursor.CROSSHAIR);
MenuBar menuBar = new MenuBar();
Menu menu = new Menu("Window");
menu.getItems().add(new MenuItem("browser"));
menu.getItems().add(new MenuItem("img"));
menuBar.getMenus().add(menu);
menuBar.prefWidthProperty().bind(primaryStage.widthProperty());
BorderPane borderPane = new BorderPane();
borderPane.prefHeightProperty().bind(scene.heightProperty());
borderPane.prefWidthProperty().bind(scene.widthProperty());
borderPane.setTop(menuBar);
root.getChildren().add(borderPane);
primaryStage.setScene(scene);
primaryStage.show();
}
here is the code part where is im adding menu bar with border pane but its hang my application as im not able to login or do anythng and i had to add child window also for reference im attaching the image
This is a very basic and minimal example of handling views in the center of a BorderPane by MenuItems:
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.layout.BorderPane;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
public class Main extends Application {
#Override
public void start(Stage primaryStage) {
try {
BorderPane root = new BorderPane();
// create the menu bar with a menu and its items
MenuBar menuBar = new MenuBar();
Menu mainMenu = new Menu("Window");
MenuItem browserItem = new MenuItem("browser");
MenuItem imageItem = new MenuItem("image");
MenuItem closeItem = new MenuItem("exit");
// create some different contents for the center of the border pane
Label imagePlaceHolder = new Label("IMAGE TO BE SHOWN");
WebView browser = new WebView();
WebEngine browserEngine = browser.getEngine();
// set the actions for the different items
closeItem.setOnAction(action -> {
System.exit(0);
});
imageItem.setOnAction(action -> {
root.setCenter(imagePlaceHolder);
});
browserItem.setOnAction(action -> {
root.setCenter(browser);
browserEngine.load("http://www.google.com");
});
// add items to the menu, then the menu to the menu bar
mainMenu.getItems().addAll(closeItem, browserItem, imageItem);
menuBar.getMenus().add(mainMenu);
// set the scene
Scene scene = new Scene(root,400,400);
root.setTop(menuBar);
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
I hope it helps…
EDIT
I just saw your newest edit… In case you really need different windows (scenes or stages), the approach will get more complex. Readers have to get more information about the different windows (like how you create them, handle their contents and more).

how to pause sequential transition of JAVAFX

Code below show part of the function of my sequential transition,i wish to use Pslideshow.pause() to enable user to pause the slide and also continue play the slide.
But i realised, once i use Pslideshow.play() , the status of the Pslideshow change into Stopped immediately when i clicked on the pane which i create to display the slide.
What should i do in order to allow user to pause and play again my sequential transition? Thanks in advance!
public void start(){
for (Label slide : LabelSlides) {
SequentialTransition sequentialTransition = new SequentialTransition();
FadeTransition fadeIn = getFadeTransition(slide, 0.0, 1.0, 2000);
PauseTransition stayOn = new PauseTransition(Duration.millis(2000));
FadeTransition fadeOut = getFadeTransition(slide, 1.0, 0.0, 2000);
sequentialTransition.getChildren().addAll(fadeIn, stayOn, fadeOut);
slide.setOpacity(0);
this.root.setStyle("-fx-background-color: Black");
this.root.getChildren().add(slide);
Pslideshow.getChildren().add(sequentialTransition);
}
Pslideshow.play();
}
#FXML
public void PlaySlide(ActionEvent event) throws IOException
{ Node node=(Node) event.getSource();
Stage stage=(Stage) node.getScene().getWindow();
Stage newStage = new Stage();
newStage.setWidth(stage.getWidth());
newStage.setHeight(stage.getHeight());
Scene_3Controller simpleSlideShow = new Scene_3Controller();
Scene scene = new Scene(simpleSlideShow.getRoot());
MediaP.play();
simpleSlideShow.start();
scene.setOnMouseClicked(new ClickHandler());
newStage.setScene(scene);
newStage.show();
}
The code is copied from the SequentialTransitionStatus gist that I created to explain how SequentialTransition can change different status i.e. RUNNING, PAUSED, STOPPED.
import javafx.animation.FadeTransition;
import javafx.animation.PauseTransition;
import javafx.animation.SequentialTransition;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.Duration;
public class SequentialTransitionStatus extends Application {
#Override
public void start(Stage primaryStage) {
Label slide = new Label("slide");
slide.setOpacity(0);
FadeTransition fadeIn = new FadeTransition(Duration.millis(2000), slide);
fadeIn.setFromValue(0);
fadeIn.setToValue(1);
PauseTransition stayOn = new PauseTransition(Duration.millis(1000));
FadeTransition fadeOut = new FadeTransition(Duration.millis(2000), slide);
fadeOut.setFromValue(1);
fadeOut.setToValue(0);
SequentialTransition sequentialTransition = new SequentialTransition();
sequentialTransition.getChildren().addAll(fadeIn, stayOn, fadeOut);
Label status = new Label();
status.textProperty().bind(sequentialTransition.statusProperty().asString());
Button play = new Button("Play");
play.setOnAction(event -> sequentialTransition.play());
Button pause = new Button("Pause");
pause.setOnAction(event -> sequentialTransition.pause());
HBox hBox = new HBox(10, play, pause);
hBox.setAlignment(Pos.CENTER);
VBox box = new VBox(20, slide, hBox, status);
box.setAlignment(Pos.CENTER);
Scene scene = new Scene(box, 300, 250);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}

JavaFX setTabcontent dynamically

I have made an application which uses tabpane. I am able to set tooltip and title of each tab dynamically. But how do I set its contents dynamically. I am sure that I can maintain a list of Node object and add it to tab during iteration, but I feel there are other ways to do it. Here is what I have done so far.
public class Index extends Application {
public static void main(String[] args) {
launch(args);
}
final String[] tabContent={"title1"
,"title2"
,"title3"
,"title4"
,"title5"};
final String[] tabToolTip={"tooltip1"
,"tooltip2"
,"tooltip3"
,"tooltip4"
,"tooltip5"};
#Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Ipas Utility");
Group root = new Group();
Scene scene = new Scene(root, 1000, 600, Color.ALICEBLUE);
TabPane tabPane = new TabPane();
tabPane.setTooltip(new Tooltip("Hover on each tab for an overview"));
BorderPane borderPane = new BorderPane();
for (int i = 0; i < 5; i++) {
Tab tab = new Tab();
tab.setText(tabContent[i]);
tab.setClosable(false);
tab.setTooltip(new Tooltip(tabToolTip[i]));
HBox hbox = new HBox();
hbox.getChildren().add(new Label(tabContent[i]));
hbox.setAlignment(Pos.CENTER);
tab.setContent(hbox);;
tabPane.getTabs().add(tab);
}
// bind to take available space
borderPane.prefHeightProperty().bind(scene.heightProperty());
borderPane.prefWidthProperty().bind(scene.widthProperty());
borderPane.setCenter(tabPane);
root.getChildren().add(borderPane);
primaryStage.setScene(scene);
primaryStage.show();
}
}
How I can maintain tabcontent in a list/array outside start block?
That's quite okay what you did. Here's a modified version of your code which allows you to add a tab on button click:
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.control.Tooltip;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
public class Main extends Application {
private static int tabCount = 0;
#Override
public void start(Stage primaryStage) {
TabPane tabPane = new TabPane();
tabPane.getTabs().add(createTab());
tabPane.getTabs().add(createTab());
tabPane.getTabs().add(createTab());
Button addTabButton = new Button( "Add Tab");
addTabButton.setOnAction(e -> {
tabPane.getTabs().add(createTab());
});
Button logButton = new Button( "Log");
logButton.setOnAction(e -> {
for( Tab tab: tabPane.getTabs()) {
System.out.println( "Tab " + tab.getText() + " has content " + tab.getContent());
}
});
HBox toolbar = new HBox();
HBox.setMargin(addTabButton, new Insets(5,5,5,5));
HBox.setMargin(logButton, new Insets(5,5,5,5));
toolbar.getChildren().addAll( addTabButton, logButton);
BorderPane root = new BorderPane();
root.setTop(toolbar);
root.setCenter(tabPane);
Scene scene = new Scene(root, 640, 480);
primaryStage.setScene(scene);
primaryStage.show();
}
public static Tab createTab() {
tabCount++;
Tab tab = new Tab();
tab.setText("Tab " + tabCount);
tab.setTooltip( new Tooltip( "Tooltip Tab " + tabCount));
Node content = new Label( "Content Tab " + tabCount);
tab.setContent(content);
return tab;
}
public static void main(String[] args) {
launch(args);
}
}
In order to access the tabs you can use getTabs. And in order to change the content, i. e. the node that represents the content, you can use setContent.
The example code shows you how to iterate through the tabs by pressing the log button.

JavaFX Stage wont show in Android

I have deployed a JavaFX application in Android using Gluon Plugin. What Im trying to do right now is to show a second Stage, but unfortunately it wont show.
Here's the code of the second Stage:
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class Setting {
public Setting(){
Text title = new Text("Settings");
title.setId("setting-title");
title.setFont(Font.font("Arial", (int)(MainApp.HEIGHT * 0.04)));
Label label_url = new Label("URL");
label_url.setFont(Font.font("Arial", (int)(MainApp.HEIGHT * 0.03)));
Label label_style = new Label("Style");
label_style.setFont(Font.font("Arial", (int)(MainApp.HEIGHT * 0.03)));
TextField text_url = new TextField(MainApp.URL);
text_url.setFont(Font.font("Arial", (int)(MainApp.HEIGHT * 0.03)));
ComboBox style_box = new ComboBox();
style_box.setStyle("-fx-font-size:"+(int)(MainApp.HEIGHT * 0.03)+"px;");
ObservableList<String> data = FXCollections.observableArrayList();
data.add("Blue");
data.add("Red");
data.add("Yellow");
style_box.setItems(data);
GridPane grid = new GridPane();
grid.setVgap(5);
grid.setHgap(5);
grid.add(label_url, 0, 0);
grid.add(label_style, 0, 1);
grid.add(text_url, 1, 0);
grid.add(style_box, 1, 1);
VBox root = new VBox();
root.setAlignment(Pos.CENTER);
root.getChildren().addAll(title,grid);
Scene scene = new Scene(root, MainApp.WIDTH * 0.6, MainApp.HEIGHT * 0.4);
if(MainApp.SETTING == null){
MainApp.SETTING = new Stage();
MainApp.SETTING.setScene(scene);
MainApp.SETTING.initOwner(MainApp.MAINSTAGE);
MainApp.SETTING.setTitle("Settings");
}
}
public void show(){
if(!MainApp.SETTING.isShowing()) MainApp.SETTING.show();
}
}
This code works when I tried to run it as a desktop application. Im using Android 4.2.
Using JavaFXPorts you can change scenes and stages while running on Android.
I've modified the Gluon plugin for NetBeans default sample to achieve this, first switching scenes:
private Scene mainScene;
#Override
public void start(Stage stage) {
Rectangle2D visualBounds = Screen.getPrimary().getVisualBounds();
Label label = new Label("Main Scene");
StackPane root = new StackPane(label);
mainScene = new Scene(root, visualBounds.getWidth(), visualBounds.getHeight());
stage.setScene(mainScene);
stage.show();
label.setOnMouseClicked(e->{
Label labelSettings = new Label("Settings Scene. Click to go back");
StackPane rootSettings = new StackPane(labelSettings);
Scene settingsScene = new Scene(rootSettings, visualBounds.getWidth(), visualBounds.getHeight());
stage.setScene(settingsScene);
labelSettings.setOnMouseClicked(t->stage.setScene(mainScene));
});
}
Now switching stages:
private Scene mainScene;
#Override
public void start(Stage stage) {
Rectangle2D visualBounds = Screen.getPrimary().getVisualBounds();
Label label = new Label("Main Stage");
StackPane root = new StackPane(label);
root.setStyle("-fx-background-color: aquamarine;");
mainScene = new Scene(root, visualBounds.getWidth(), visualBounds.getHeight());
stage.setScene(mainScene);
stage.setTitle("Main Stage");
stage.show();
label.setOnMouseClicked(e->{
Label labelSettings = new Label("Settings Stage. Click to go back");
StackPane rootSettings = new StackPane(labelSettings);
rootSettings.setStyle("-fx-background-color: burlywood;");
Scene settingsScene = new Scene(rootSettings, visualBounds.getWidth(), visualBounds.getHeight());
Stage stage2 = new Stage();
stage2.setScene(settingsScene);
stage2.show();
labelSettings.setOnMouseClicked(t->stage2.hide());
});
}
And there's another possibility: you can use Gluon Charm, to manage different views.
Have a look at this project to get you started.

Categories

Resources