JavaFX communication between controllers 📣
ฝัง
- เผยแพร่เมื่อ 12 ธ.ค. 2024
- Javafx pass values between controllers
#javafx
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);
}
}
//-----------------------------------------Scene1.fxml-----------------------------------------
//-----------------------------------------Scene2.fxml-----------------------------------------
//----------------------------------Scene1Controller.java-----------------------------------
package application;
import java.io.IOException;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
public class Scene1Controller {
@FXML
TextField nameTextField;
private Stage stage;
private Scene scene;
private Parent root;
public void login(ActionEvent event) throws IOException {
String username = nameTextField.getText();
FXMLLoader loader = new FXMLLoader(getClass().getResource("Scene2.fxml"));
root = loader.load();
Scene2Controller scene2Controller = loader.getController();
scene2Controller.displayName(username);
//root = FXMLLoader.load(getClass().getResource("Scene2.fxml"));
stage = (Stage)((Node)event.getSource()).getScene().getWindow();
scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
}
//----------------------------------Scene2Controller.java-----------------------------------
package application;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
public class Scene2Controller {
@FXML
Label nameLabel;
public void displayName(String username) {
nameLabel.setText("Hello: " + username);
}
}
//--------------------------------------------------------------------------------------------------
Can you make a video about good practices/design patterns for data validation from UI and comunication with business logic ???? I've never seen a video about it, so you can be the chosen one for this job :')
We need more javafx videos ,you do the job very simple and easy to understand thanks professor
I was just searching for some Java tutorials and this channel has the best one. I am a fellow bro now :D
It's true ,it brings me a lot Just fellow lessons
Once again your video saved me from missing a deadline in uni ! bro code your my favorite programing teacher
This is so much simpler than how my professor demonstrated. Thanks bro
This is the best Java channel I've found so far!
Really easy to follow and informative videos about JavaFX. Thanks for making these videos
If you get an error when clicking the Login button, make sure that your Scene2.fxml document has Scene2Controller as its controller.
this comment saved me
Love all your videos! There aren’t enough JAVAFX videos up!
You explain so well dude! And you even go directly to the point, thanks for making my time worth :D
good job sub++ from Argentina :p
Thanks dude, you're a god, best tutorials ever, like and recommend!
I wish i found this video a month ago. Keep up the good work bro
You saved my coding skin! Thank you so much, man!
I don't know why I run into a NullPointerException at this line of code:
scene2Controller.displayName(userName);
Edit: Never mind! I just forgot to add Controller class
i am getting error can you share what fixed yours
@@abhisheksahu6421 You need to link your Controller in SceneBuilder
@@lequangnghi194 thanks
thank you!!
i am unable to fix it can you tell me the clear solution
Amazing tutorial! Thank you very much for this video
Bro you are really the goat
easy and simple to follow, even one can unders
tand step without voice noted embeded
You saved me. Thanks a lot.
Thanks bro! This tips is very helpful to me
Best one out there!
Thanks Bro, that helps a lot!
Excellent Useful Thank you very much
I am a fellow bro ....Yes boss
Great video!
Useful 👌👍
From today, I'm a fellow bro :-)
Bro adamdır!
Thank you, bro!
Love it.
love you bro
Thanks this helped me alot
Nice🙂
awesome!
Thanks you so much !
Please be the next media player tutorial (audio and video) I will only learn from your channel
Hello! Hoping for some help, I'm having issues getting an ClassCastException with the Scene2Controller scene2controller = loader.getcontroller(); piece, the only thing i'm doing differently is using a package, could that be effecting how this should work?
Bro, i watch this video now and i have the same problem. If actuality: "Scene2.fxml" have , u should change
nice show()
Nice video
I hate JavaFX. I would much rather work with Swing. It's baffling that I have to create a class, create an XML file(excuse me, an "FXML" file) for the class to load, then specify in the FXML file that I want to have these methods and objects. It's super redundant, but it's easier to update and modify than the Swing GUIs, so I guess employers like it for that reason.
hey please! how can I just get the data in the same way but to more than just one controller???
like i wanna get that same data from scene 1 to scene 2 , scene 3, and scene 4 ???
please help !!
Omg finally was able to shut down that executor from the controller file on app exit
Thanks, I Love you
What about if I want to update a method in differnt controller from another controller
da best
thanks bro
Hell yeah, thanks bro code
Thanks bro.
Heey, im not sure if you can respond as quickly as possible, but how do you pass boolean values of Radio Buttons to another controller? I want to do something if a certain Radio Button is clicked.
Hey Mate thanks for the vid!
Can you link your Eclipse theme? I have already a black one, but yours look better and the overlay (HUD) with the projects are also black and not (like now) only the main window with the code.
Thanks for sharing this video , it's really helpful. Only one question that my scene1 login button can't link to login action.
no drop-down available and if I type "login" manually show javafx.fxml.LoadException: No controller specified
You need to choose the Controller class from the left bottom section, below the Hierarchy, called Controller. Then you will see the drop-down menu and the variables to link.
Thanks.
thank you bro
Thanks!!!!!!!!!😁
Thank you
can you do a java dao toturial with practical example
not a bad idea!
for some reason it shows Node cannot be resolved to a type in the main controller class
But how can you do this with a label and not passing information through a text box?
I love Bro Code
good :)
How can I use Javafx to drawing line by using dda algorithm.¿
plz help me i have this problem class com.example.demo1.Nouvauxlocataire cannot be cast to class javafx.scene.Parent
TH-cam doesn't notify this video to me :c
I'm a faaaan of yours
class com.example.demo1.Nouvauxlocataire cannot be cast to class javafx.scene.Parent
Tnx bro
I have a question
lets say i wanna let the user put his first name and his last name
so I change the scene2Controller to:
public void displayerName (String username, String theLastName){
nameLabel.setText("hello:"+username +" "+ theLastName);
}
and the scene1Contoller to a:
theHelloSceneController.displayerName(username , theLastName);
with of course adding
String theLastName = userlastName.getText();
put it doesn't log in and move to the Scene2
what should i do
please help
Does not work for me i get a big error
Nvm Got it to work :D
(btw your tutorials are so fun and good to watch thank you!)
i got an exception ...whenever i clicked login...i used the same code..can u help me out plz
i want the src code plz its urgent
i dont really know why everyone uses fxml instead of Java it self to build things this is not the only course that does this and i wanna learn java not xml ..:(
then use Scene builder... using java to build is a really tedious process, and becomes unmanageable if youre seriously developing an application
Do you use discord?
I do for personal use, but I don't have a server set up for this channel
It not work
Please kotlin
Thank you bro