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);
}
}
Related
I have integrated a JFileChooser into a JavaFX application using a SwingNode. The dialog displays and is usable, but I am unsure about how to get the selected file from it.
Thanks for any help.
#FXML
public void openDialog(MouseEvent event) {
SwingNode swingNode = new SwingNode();
SwingUtilities.invokeLater(() -> {
swingNode.setContent(fileChooser);
});
BorderPane pane = new BorderPane();
pane.setCenter(swingNode);
Stage stage = new Stage();
Scene scene = new Scene(pane, 500, 500);
stage.setScene(scene);
stage.show();
}
Out of curiosity I made a small example to do this.
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.embed.swing.SwingNode;
import javax.swing.JFileChooser;
import javax.swing.SwingUtilities;
public class JunkFx extends Application {
public void openDialog(MouseEvent event) {
JFileChooser fileChooser = new JFileChooser("why");
SwingNode swingNode = new SwingNode();
SwingUtilities.invokeLater(() -> {
swingNode.setContent(fileChooser);
});
BorderPane pane = new BorderPane();
pane.setCenter(swingNode);
Stage stage = new Stage();
Scene scene = new Scene(pane, 500, 500);
stage.setScene(scene);
stage.show();
fileChooser.addActionListener(evt->{
System.out.println(evt.getActionCommand());
System.out.println(fileChooser.getSelectedFile());
Platform.runLater(stage::hide);
});
}
#Override
public void start(Stage stage) throws Exception {
Button b = new Button("click");
b.setOnMouseClicked(this::openDialog);
Scene scene = new Scene(new StackPane(b), 640, 480);
stage.setTitle("open a dialog");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args){
launch(args);
}
}
There will be a response on the EDT, then you have to manage what to do with it, checking for cancelled or accepted etc. then fire an action onto the Platform thread.
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.
I have a stage that acts as popup,I want to close the stage if the user clicks outside that stage,how to achieve that,My code for the stage acting as popup is as follows:
Stage stage = new Stage();
Parent root = FXMLLoader.load(getClass().getResource("colorchange.fxml"));
Scene sce = new Scene(root,400,400);
stage.initModality(Modality.APPLICATION_MODAL);
stage.initOwner(color.getScene().getWindow());
stage.setScene(sce);
stage.showAndWait();
Register a listener with stage.focusedProperty() and call stage.hide() if the focused property changes to false.
SSCCE:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Modality;
import javafx.stage.Stage;
public class CloseWindowOnClickOutside extends Application {
#Override
public void start(Stage primaryStage) {
Button showPopup = new Button("Show popup");
showPopup.setOnAction(e -> {
Stage popup = new Stage();
Scene scene = new Scene(new Label("Popup"), 120, 40);
popup.setScene(scene);
popup.initModality(Modality.APPLICATION_MODAL);
popup.initOwner(primaryStage);
popup.focusedProperty().addListener((obs, wasFocused, isNowFocused) -> {
if (! isNowFocused) {
popup.hide();
}
});
popup.show();
});
StackPane root = new StackPane(showPopup);
Scene scene = new Scene(root, 350, 120);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
I am making a web browser in JavaFX and I thought everything was good and dandy. I ran the application and now the History and Bookmarks buttons will not appear. I looked through the code and saw no errors. How do I fix this?
package javafxapplication3;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.application.Application;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;
import javafx.scene.web.WebHistory;
public class CreateAsIGo2 extends Application{
public static void main(String[] args){
launch(args);
}
public void start(Stage primaryStage){
BorderPane ap = new BorderPane();
BorderPane ap2 = new BorderPane();
BorderPane ap3 = new BorderPane();
Scene scene = new Scene(ap, 700, 700);
Scene scene2 = new Scene(ap2, 700, 700);
Scene scene3 = new Scene(ap3, 700, 700);
VBox sp = new VBox();
VBox sp2 = new VBox();
VBox sp3 = new VBox();
Button HistoryButton = new Button("History");
Button BookmarksButton = new Button("Bookmarks");
Button RefreshButton = new Button("Refresh");
Button BackButton = new Button("Back");
Button BackToBrowser = new Button("Back to surfing the web");
Button ForwardButton = new Button("Forward");
TextField tf = new TextField();
tf.setPromptText("URL");
WebView browser = new WebView();
WebEngine webEngine = browser.getEngine();
webEngine.load("http://www.google.com");
webEngine.setJavaScriptEnabled(true);
WebHistory history = webEngine.getHistory();
HistoryButton.setOnAction(e -> primaryStage.setScene(scene2));
BookmarksButton.setOnAction(e -> primaryStage.setScene(scene3));
RefreshButton.setOnAction(e -> webEngine.reload());
BackButton.setOnAction(e -> webEngine.executeScript("history.back()"));
BackToBrowser.setOnAction(e -> primaryStage.setScene(scene));
ForwardButton.setOnAction(e -> webEngine.executeScript("history.forward()"));
tf.setOnKeyPressed((KeyEvent ke) -> {
KeyCode key = ke.getCode();
if(key == KeyCode.ENTER){
webEngine.load("http://" + tf.getText());
}
});
sp.getChildren().addAll(HistoryButton, BookmarksButton, RefreshButton, BackButton, ForwardButton);
sp2.getChildren().addAll(BookmarksButton, BackToBrowser);
sp3.getChildren().addAll(HistoryButton, BackToBrowser);
ap.setRight(sp);
ap2.setRight(sp2);
ap3.setRight(sp3);
ap.setTop(tf);
ap.setCenter(browser);
browser.setPrefSize(700, 700);
primaryStage.setTitle("JTG Browser Alpha");
primaryStage.setScene(scene);
primaryStage.show();
}
}
See the node documentation:
If a program adds a child node to a Parent (including Group, Region, etc) and that node is already a child of a different Parent or the root of a Scene, the node is automatically (and silently) removed from its former parent.
You are adding your HistoryButton (and other buttons) to different scenes and, when you do so, they are automatically removed from the previous scenes. You need to create new button instances if you want them visible in every scene.
Small aside: it is best to follow Java naming conventions, e.g. historyButton instead of HistoryButton.
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.