JavaFX switch scenes 💞
HTML-код
- Опубликовано: 10 янв 2021
- JavaFX switch scenes with using SceneBuilder tutorial example explained
#javafx #switch #scenes
//--------------------------------Main.java--------------------------------------
package application;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;
public class Main extends Application {
@Override
public void start(Stage stage) {
try {
Parent root = FXMLLoader.load(getClass().getResource("Scene1.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
//---------------------------------SceneController.java---------------------------------------
package application;
import java.io.IOException;
import javafx.event.ActionEvent;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class SceneController {
private Stage stage;
private Scene scene;
private Parent root;
public void switchToScene1(ActionEvent event) throws IOException {
root = FXMLLoader.load(getClass().getResource("Scene1.fxml"));
stage = (Stage)((Node)event.getSource()).getScene().getWindow();
scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
public void switchToScene2(ActionEvent event) throws IOException {
Parent root = FXMLLoader.load(getClass().getResource("Scene2.fxml"));
stage = (Stage)((Node)event.getSource()).getScene().getWindow();
scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
}
//------------------------------------------------------------------------------------------------
Bro Code merch store 👟 :
===========================================================
teespring.com/stores/bro-code-5
===========================================================
music credits 🎼 :
===========================================================
Up In My Jam (All Of A Sudden) by - Kubbi / kubbi
Creative Commons - Attribution-ShareAlike 3.0 Unported- CC BY-SA 3.0
Free Download / Stream: bit.ly/2JnDfCE
Music promoted by Audio Library • Up In My Jam (All Of A...
=========================================================== - Наука
//--------------------------------Main.java--------------------------------------
package application;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;
public class Main extends Application {
@Override
public void start(Stage stage) {
try {
Parent root = FXMLLoader.load(getClass().getResource("Scene1.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
//---------------------------------SceneController.java---------------------------------------
package application;
import java.io.IOException;
import javafx.event.ActionEvent;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class SceneController {
private Stage stage;
private Scene scene;
private Parent root;
public void switchToScene1(ActionEvent event) throws IOException {
root = FXMLLoader.load(getClass().getResource("Scene1.fxml"));
stage = (Stage)((Node)event.getSource()).getScene().getWindow();
scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
public void switchToScene2(ActionEvent event) throws IOException {
Parent root = FXMLLoader.load(getClass().getResource("Scene2.fxml"));
stage = (Stage)((Node)event.getSource()).getScene().getWindow();
scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
}
//-----------------------------------------Scene1.fxml-------------------------------------------------
//-----------------------------------------Scene2.fxml-------------------------------------------------
//------------------------------------------------------------------------------------------------------
I accidentally typed .getScene(scene); instead of .setScene(scene); and it took me 10 minutes to fix it.
Am I doomed at coding?
@@catharperfect7036 you're doomed if you stop.
You literally uploaded this just in time!!! I was wondering how I could switch scenes in JavaFX, because I'll need to work on a project coming up. Let me tell you, if the stuff in this video works on my PC, you've just saved the entire thing for me! Thank you!
You just saved the day (23:25). Been working on this all afternoon, my head is going to explode. Been looking in all my books, been watching all my usual tutorials and some others, your code is the only one that actually worked. THANKS ++++ I learned that you do not have access to the Controller Panel on the bottom left part of the SceneBuilder when you open it in IntelliJ. You can only access it within SceneBuilder itself.
Love this video. I was wondering how to do this switching the scenes, now thanks to you I'm enlightened:)
The best teacher ever, thank you!
The best one who do programming
I like all your videos about javaFx ...thanks for share
Really appreciate the video, even though scene builder isn't popular content. Helped me a ton brother!
Bro I'm writing a quick, trash GUI for a project and you've been a LIFE SAVER. Love you
the way you explain is awesome! thank you!
this course is really helpful
Its a great tutorial and all but theres a lot of stuff that i don't understand that wasn't explained... Especially the five lines of code that are in the methods for switching scene. I would've liked it a lot more if there was a bit more explanation as to why you wrote the lines that you did. thanks
You are the best. Keep going. I am glad I've found your channel
I am now learning all from you Thank you once again
Thank you so much so the Javafx tutorials! you are a lifesaver
much respect and gratitude to you bro💯
Finally got it!!! Thank you my dudee!!!!
100% useful for begginers 😁 thanks
Loving this Series
Thank you very much for this video. I have been struggling for the past two nights and could not find the solution until I watched this one.
great content, exactly what I was looking for
Amazing, you made things so easy!
Thank You @Bro Code. Finally I found a simple solution. Thank for sharing this video :)
that’s a great video! thanks u really help me)
i like your video,i am learing javafx from you
It was soo helpful and fun toooo
amazingley amazing
thank you, just thanks for this video ❤
I appreciate your video! It provided me with significant assistance. Greetings from Vienna!
Keep up the awesome work!
Thank you so much! This helped a lot! God bless :)
Nice videos keep up the good work!
Muchas gracias por el codigo, me ayudo mucho a entender este procesoooo
Thank you sooo much
Really helpful. Thanks a lot.
Go ahead,excellent
Excellent!! Thank you!1
WOW you are amazing
Hi thanks for the tutorial I am subing
Useful 👌👍
thank you very much :D
from deep of my heart
Wonderful, thank you
Thank you so much, bro! x)
Thank you. It helped me :)
Muchas gracias. Te felicito, tienes una forma muy agradable de enseñar.
great tutorial
Thank you 😭💕💞
this guys the goat
Perfect man, thanks a lot
Life saver❤️❤️❤️❤️❤️❤️🙏👍
bro's the best
da best
superb!
Encontré lo que necesitaba en este video, gracias.
good work
helped a lot, thanks!
Awesome
Excellent vid
That was great, thank you
great!!
great ivd bor
GR8 VIDEO M8!
THANKS!!!
Thanki you very interesting
You are beautiful! THANK YOU BRO!
Love your content! this really helped! could you make a tutorial on Windowbuilder?
Nice video man !
gg bang,karena video ini tugas akhir sekolahku telah berjalan😁😁
Thanks a lot
bro , you are so good
Thank you bro
Great video! Can you do a JavaScript tutorial?
Thank you!!
Very well!!!! Muito bom!!!!
Legal! Legal!
thank you bro
Thank youuuuu!!!
nice video
great, thank you!
Thanks bro😃😂
I did it, man thanks!
It's great thx!
HOLY FUCKING SHIT, the scene/screen switch is EXACTLY the problem that stumped me earlier on my first serious project, so much so that it really killed my motivation at that time, and everything I could find on the subject was something monstrously complicated and big that I couldn't adapt and make work.
I'll need to test this myself, of course, but... this looks so concise. This looks REALLY concise. I still have the question of "can this work if each of the two methods is in its own separate controller?" (so that you don't have to have the same controller for all the scenes). but other than that... this is so concise. And the only complex part that I'm not fully understanding yet is the Stage/Node/event cast, which is a lot of casting and chaining and I'll need to read the docs more carefully to see how and why this works, but if this works out for me... holy shit, this is what I needed. I think this is what I needed.
Thanks a lot, man.
EDIT: Update, did it slightly differently (with some other help), but in a similar way. I'm not sure if this is the best option, but this helped A LOT. Thank you.
do you mind helping me out by saying what way you did it? because i can't get the scenes to swtich, it gives me pointer null error.
@@hunchortw Not sure you'll see this but what solved it for me was putting the fxml files in the resources file. No problems after that.
Best
👍
ty
thanks dude
YAY
thanks bro
thank you so much it works perfectly , but can i send data from scenes using this method ?
Great video!
Thank you Dear
thanks!
Another comment because you're saving my university life
Thanks bro 👌
Any reason why you don't need to add @FXML before the switchToScene1 and 2 function? I had to to make it work. Thanks.
what about going back to scene 1 without making new scene ?
This is a good question, because sometimes we want to our software to be less resource-intensive. This is my approach:
public class SceneController {
static private Stage stage;
static private Scene scene1;
static private Scene scene2;
@FXML
private void switchToScene1(ActionEvent event) {
stage.setScene(scene1);
stage.show();
System.out.println("Stage: " + stage);
System.out.println("Scene 1: " + scene1);
}
@FXML
private void switchToScene2(ActionEvent event) throws IOException {
if (scene2 == null) {
Parent root = FXMLLoader.load(this.getClass().getResource("scene2.fxml"));
scene2 = new Scene(root);
scene1 = ((Node)event.getSource()).getScene();
stage = (Stage) scene1.getWindow();
}
stage.setScene(scene2);
stage.show();
System.out.println("Stage: " + stage);
System.out.println("Scene 2: " + scene2);
}
}
So this is how one should create a modular Ui? Scenes are a collection of linked Ui elements
Im back to this video with additional question. Bro how to measure the time between switching scenes in mili or nanoseconds? I know for this example it is nearly 0 but for more complicated memory-eating scenes?
Bussin
Thanks Thanks 😊
thanks
Thank you !!!
Thanks bro