Can we change the scene in Java Scene Builder 2.0 without using button after 5 seconds - scenebuilder

Basically i am using JavaFX scene buidler 2.0 i want to change the scene from one to other without using any button for them.
Main File
public class OurFirstProject extends Application {
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
stage.setFullScreenExitHint("");
//stage.setFullScreenExitKeyCombination(KeyCombination.NO_MATCH);
Rectangle2D screenBounds = Screen.getPrimary().getVisualBounds();
Scene scene = new Scene(root, screenBounds.getWidth(), screenBounds.getHeight());
stage.setScene(scene);
stage.setFullScreen(true);
stage.show();
public static void main(String[] args) {
Application.launch(args);
}
}
public class FXMLDocumentController implements Initializable {
#FXML
private void change(ActionEvent event) throws IOException {
Parent sceneChange = FXMLLoader.load(getClass().getResource("Change.fxml"));
Rectangle2D screenBounds = Screen.getPrimary().getVisualBounds();
Scene changeScene = new Scene(sceneChange, screenBounds.getWidth(), screenBounds.getHeight());
Stage Window = (Stage) ((Node) event.getSource()).getScene().getWindow();
Window.setScene(changeScene);
Window.setFullScreen(true);
Window.show();
}
int a = 0;
#FXML
public Button helloButton;
#FXML
private Label ourLabel;
#FXML
private void printHello(ActionEvent e) {
a++;
if (a % 2 == 0) {
ourLabel.setText("Hello World! Kyun" + a);
} else {
ourLabel.setText("Hello Dunia" + a);
}
}
#Override
public void initialize(URL url, ResourceBundle rb) {
enter code here
}
The Scene Mentioned in the FXML File Name"change", i want to Run this Scen without Using Button I wanna run this on the delay of five second of FIrst scene.

You can use a Timer() like,
Timer timer = new Timer();
timer.schedule(new TimerTask() {
#Override
public void run() {
Platform.runLater(new Runnable() {
#Override
public void run() {// After this you can add your change.fxml load code
Parent root = null;
try {
root = fxmlLoader.load();
}catch(Exception e)
{
//Exception catch code here
}
primaryStage.show();//Here you can write your show code like window.show()
}
});
}
},5000);// 5000- time delay in milliseconds

Related

ProgressIndicator finishes when I start another Stage

I want to create an application starter. The ProgressIndicator should running during start of the new Stage in new Thread. But if I click at the start button the ProgressIndicator stops running.
It works if I make some I/O and ProgressIndicator shows progress. It is apparently not possible to update two stages in JavaFX in parallel or does anyone have a solution for me?
public class Main extends Application {
public void start(Stage primaryStage) {
primaryStage.setScene(new Scene(new UserVC().getView(), 600, 200));
primaryStage.show();
userVC.autologin();
}
public static void main(String[] args) { launch(args); }
}
public class UserView extends BorderPane {
private ProgressIndicator progressIndicator = new ProgressIndicator();
public UserView() {
super.setCenter(createContent());
}
public Node createContent() {
HBox userbox = new HBox();
userbox.getChildren().add(progressIndicator);
progressIndicator.show();
return userbox;
}
}
public class UserVC {
private UserView view = new UserView();
public UserVC() {
}
public void autologin() {
Task<Void> task = new Task<Void>() {
#Override public Void call() throws InterruptedException {
try {
Thread.sleep(1000);
Platform.runLater(new Runnable() {
#Override public void run() {
Stage stage = new Stage();
stage.setScene(new Scene(new MainControlVC().getView(), 900, 300));
stage.show();
}
});
}
catch(Exception e) {
e.printStackTrace();
}
return null;
}
};
view.getProgressIndicator().progressProperty().unbind();
view.getProgressIndicator().progressProperty().bind(task.progressProperty());
new Thread(task).start();
}
}
I changed the Task to Thread and it works fine now:
Thread thread = new Thread() {
public void run() {
mainController = new MainControlVC(dataDirectory, user);
Platform.runLater(new Runnable() {
#Override public void run() {
Stage stage = new Stage();
Scene scene = new Scene(mainController.getView(), mainController.getWidth(), mainController.getHeight());
stage.setScene(scene);
stage.show();
}
});
}
};
thread.start();

Switching between Scene JavaFX

I created a scene in class1, then i created a scene2 in class2.
How to switch between them?
public class class1 extends Application{
Stage window1;
BorderPane layout1;
Scene scene1;
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) throws Exception {
window1 = primaryStage;
window.setTitle("Stage 1");
// And here is a button which switchs between scenes or stages,
//i dont know what is better.So:
button.setOnAction(e -> ?????????)
scene1 = new Scene(layout1, 800,600);
window1.show();
}
}
And here is the second class in which i have another scene.
public class class2 extends Application{
Stage window2;
BorderPane layout2;
Scene scene2;
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) throws Exception {
window2 = primaryStage;
window2.setTitle("Stage 2");
scene2 = new Scene(layout, 800,600);
window2.show();
}
}
I wrote this controller to keep track of the different scenegraphes and switch the content of my Stage with no hassle.
Maybe you want to take a look at FXML:
http://docs.oracle.com/javafx/2/fxml_get_started/why_use_fxml.htm#CHDCHIBE
public class ScreenController {
private HashMap<String, Pane> screenMap = new HashMap<>();
private Scene main;
public ScreenController(Scene main) {
this.main = main;
}
protected void addScreen(String name, Pane pane){
screenMap.put(name, pane);
}
protected void removeScreen(String name){
screenMap.remove(name);
}
protected void activate(String name){
main.setRoot( screenMap.get(name) );
}
}
So I can write:
ScreenController screenController = new ScreenController(scene);
screenController.add("layout1", layout1 );
screenController.add("layout2", layout2 );
screenController.add("testSwitch", FXMLLoader.load(getClass().getResource( "TestSwitch.fxml" )));
button.setOnAction(e -> screenController.activate("layout2"));
This was a workaround for a fullscreen application, where the MacOS fullscreen transition was shown every time a stage switches its scene.
put this in your Controller. You can use anything (like button,label etc.) to get access to your Stage (best inside an eventhandler from Button).
Stage stage = (Stage) AnyThingOnScene.getScene().getWindow();
Parent root = FXMLLoader.load(getClass().getResource("someFile.fxml"));
Scene scene = new Scene(root,420,360);
stage.setScene(scene);

Switching scenes in JavaFX dynamically

I want to switch between 2 different scenes:
Scene 1: ScrollPane where I load different images.
Scene 2: the stage is transparent and there's only 1 button to return to Scene 1.
I´ve been able to do it just having a different fxml for each scene and creating a new scene every time I switch them.
The problem is when I switch from scene 2 to scene 1, all the images loaded in scene 1 aren't there (It´s obvious as I´m creating a new scene rather than "loading" Scene1.
Is there any way to keep the images already loaded when I swith from Scene 2 to Scene 1?
Scene1
public class ControllerImpl implements Initializable, ControlledScreen {
ScreensController myController;
public void initialize(URL url, ResourceBundle rb) {
}
public void setScreenParent(ScreensController screenParent){
myController = screenParent;
}
#FXML
private void goToScreen2(ActionEvent event) throws Exception{
try {
Stage primaryStage = (Stage) hideStage.getScene().getWindow();
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/arrow.fxml"));
Parent root1 = fxmlLoader.load();
primaryStage.setScene(new Scene(root));
}
catch (Exception e) {
e.printStackTrace();
}
}
#FXML
public javafx.scene.control.Button hideStage;
}
Scene2:
public class ControllerArrow implements Initializable{
ScreensController myController;
#Override
public void initialize(URL url, ResourceBundle rb) {
}
public void setScreenParent(ScreensController screenParent){
myController = screenParent;
}
#FXML
public javafx.scene.control.Button showStage;
#FXML
private void goToScreen1(ActionEvent event) throws Exception{
try{
Stage stage = (Stage) showStage.getScene().getWindow();
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/sample.fxml"));
Parent root = fxmlLoader.load();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
You can switch the scene of your stage like:
Stage stage = (Stage) scene.getWindow();
stage.setScene(yourScene);
So when you have an instance of your scene, you can apply it to your stage

I can't get a transparent stage in JavaFX

Here is my code, I'm trying to load a splash screen image with transparent background before my main stage starts. They come almost at the same time, but the big problem is I get a grey rectangle before anything else: .
Here is the code:
public class Menu extends Application {
private Pane splashLayout;
private Stage mainStage;
private ImageView splash;
// Creating a static root to pass to ScreenControl
private static BorderPane root = new BorderPane();
public void start(Stage splashStage) throws IOException {
final Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
this.splash = new ImageView(new Image(getClass().getResource("/splash.png").toString()));
splashStage.initStyle(StageStyle.TRANSPARENT);
showSplash(splashStage, screenSize);
// Constructing our scene using the static root
root.setCenter(new ScrollPane);
Scene scene = new Scene(root, screenSize.getWidth(), screenSize.getHeight());
showMainStage(scene);
if (splashStage.isShowing()) {
mainStage.setIconified(false);
splashStage.toFront();
FadeTransition fadeSplash = new FadeTransition(Duration.seconds(1.5), splashLayout);
fadeSplash.setDelay(Duration.seconds(3.5));
fadeSplash.setFromValue(1.0);
fadeSplash.setToValue(0.0);
fadeSplash.setOnFinished(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent actionEvent) {
splashStage.hide();
}
});
fadeSplash.play();
}
}
private void showMainStage(Scene scene) {
mainStage = new Stage(StageStyle.DECORATED);
mainStage.setTitle("book-depot");
mainStage.getIcons().add(new Image(getClass().getResourceAsStream("/icon.png")));
mainStage.setScene(scene);
mainStage.show();
}
private void showSplash(Stage splashStage, Dimension screenSize) {
splashLayout = new StackPane();
splashLayout.setStyle("-fx-background-color: transparent;");
splashLayout.getChildren().add(splash);
Scene splashScene = new Scene(splashLayout, 690, 590);
splashScene.setFill(Color.TRANSPARENT);
splashStage.setScene(splashScene);
splashStage.show();
}
public void mainGui(String[] args) {
launch(args);
}
}
Am I doing something wrong or I really can't get a transparent background?
This is what it looks like when also the other stage loads up, but I'd like it to work like that even before the main stage loads, or at least I'd want to remove the grey rectangle you can see in the other screenshot
The grey background is your "mainStage" since you are showing splash and main stages at the same time. At the beginning while showing the splash stage you can just init (not show) the main stage and show it later when the animation finishes:
public class ModifiedMenu extends Application
{
private Pane splashLayout;
private Stage mainStage;
private ImageView splash;
// Creating a static root to pass to ScreenControl
private static BorderPane root = new BorderPane();
public void start(Stage splashStage) throws IOException {
final Dimension2D screenSize = Toolkit.getDefaultToolkit().getScreenSize();
this.splash = new ImageView(new Image(getClass().getResource("/splash.png").toString()));
splashStage.initStyle(StageStyle.TRANSPARENT);
showSplash(splashStage, screenSize);
// Constructing our scene using the static root
root.setCenter(new ScrollPane());
Scene scene = new Scene(root, screenSize.getWidth(), screenSize.getHeight());
initMainStage(scene);
if (splashStage.isShowing()) {
splashStage.toFront();
FadeTransition fadeSplash = new FadeTransition(Duration.seconds(1.5), splashLayout);
fadeSplash.setDelay(Duration.seconds(3.5));
fadeSplash.setFromValue(1.0);
fadeSplash.setToValue(0.0);
fadeSplash.setOnFinished(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent actionEvent) {
splashStage.hide();
mainStage.show();
}
});
fadeSplash.play();
}
}
private void initMainStage(Scene scene) {
mainStage = new Stage(StageStyle.DECORATED);
mainStage.setTitle("book-depot");
mainStage.getIcons().add(new Image(getClass().getResourceAsStream("/icon.png")));
mainStage.setScene(scene);
}
private void showSplash(Stage splashStage, Dimension2D screenSize) {
splashLayout = new StackPane();
splashLayout.setStyle("-fx-background-color: transparent;");
splashLayout.getChildren().add(splash);
Scene splashScene = new Scene(splashLayout, 690, 590);
splashScene.setFill(Color.TRANSPARENT);
splashStage.setScene(splashScene);
splashStage.show();
}
public void mainGui(String[] args) {
launch(args);
}
}

JavaFX stage.setOnCloseRequest without function?

This is my Start-Methode. First i create a stage and set a title and a scene. And i wanna create a dialog if someone wants to close the window on the window-close-btn [X]. i thought i will catch this event with the setOnCloseRequest()-function. But i still can close all stages i open while runtime.
#Override
public void start(final Stage primaryStage) throws Exception {
primaryStage.setTitle("NetControl");
primaryStage.setScene(
createScene(loadMainPane())
);
primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
#Override
public void handle(final WindowEvent event) {
//Stage init
final Stage dialog = new Stage();
dialog.initModality(Modality.APPLICATION_MODAL);
// Frage - Label
Label label = new Label("Do you really want to quit?");
// Antwort-Button JA
Button okBtn = new Button("Yes");
okBtn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
dialog.close();
}
});
// Antwort-Button NEIN
Button cancelBtn = new Button("No");
cancelBtn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
primaryStage.show();
dialog.close();
}
});
}
});
primaryStage.show();
}
private Pane loadMainPane() throws IOException {
FXMLLoader loader = new FXMLLoader();
Pane mainPane = (Pane) loader.load(
getClass().getResourceAsStream(ContentManager.DEFAULT_SCREEN_FXML)
);
MainController mainController = loader.getController();
ContentManager.setCurrentController(mainController);
ContentManager.loadContent(ContentManager.START_SCREEN_FXML);
return mainPane;
}
private Scene createScene(Pane mainPane) {
Scene scene = new Scene(mainPane);
setUserAgentStylesheet(STYLESHEET_MODENA);
return scene;
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
Application.launch(args);
}
Are there any other functions to catch the window-events?
or isnt it logical to run the CloseRequest on the primaryStage, i read something with platforms (but i dont know if it necessary for my problem)?
In the onCloseRequest handler, call event.consume();.
That will prevent the primary stage from closing.
Remove the primaryStage.show(); call from the cancel button's handler and add a call to primaryStage.hide(); in the OK button's handler.

Resources