I'm trying to get the username that somebody uses to Login, then insert it into a db when somebody makes an apointment
This is the "Programaritest" table
ID_programare | Nume | Prenume | Data | Ora | Departament | Doctor | Username | Nr_telefon
This is the accounts table where the username is saved as Unique
ID_account | Username | Password | Email | Nume | Prenume | Admin
The input of the username is gotten from fxml via a textfield, in GetUsername method, I've pointed it out below
package LicentaApp;
public class LogareController implements Initializable {
public LoginVerifier loginVerifier = new LoginVerifier();
#FXML
private TextField Numeutilzator; Numeutilziator is the username that I am talking about
#FXML
private PasswordField Parola;
#FXML
private Label Stare;
#Override
public void initialize(URL location, ResourceBundle resources) {
if (loginVerifier.Conexiune()) {
Stare.setText("");
} else {
Stare.setText("Conexiune nereusita!");
}
}
public void Autentificare (ActionEvent event) {
try {
if(loginVerifier.testaredate(Numeutilzator.getText(), Parola.getText())) {
Stare.setText("Autentificare reusita !");
((Node)event.getSource()).getScene().getWindow().hide();
Stage StagePrincipala= new Stage();
FXMLLoader incarcator= new FXMLLoader();
Pane parinte = incarcator.load(getClass().getResource("/LicentaApp/Meniu.fxml").openStream());
Scene scene = new Scene(parinte);
scene.getStylesheets().add(getClass().getResource("Style1212.css").toExternalForm());
StagePrincipala.setScene(scene);
StagePrincipala.show();
}
else {
Stare.setText("Nume de utilizator sau parola incorect");
}
} catch (SQLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
#FXML
public void Inregistrare(ActionEvent event) {
try {
((Node)event.getSource()).getScene().getWindow().hide();
Stage PS= new Stage();
FXMLLoader incarcator= new FXMLLoader();
Pane parinte = incarcator.load(getClass().getResource("/LicentaApp/InregistrareUser.fxml").openStream());
Scene scena = new Scene(parinte);
scena.getStylesheets().add(getClass().getResource("Style1212.css").toExternalForm());
PS.setScene(scena);
PS.show();
} catch (Exception e) {
}
*Here I m calling the function that I made in AddProgramareController and
passing it the username located in Numeutilzator*
#FXML
public void GetUsername() {
try {
FXMLLoader loader=new FXMLLoader(getClass().getResource("/LicentaApp/AddProgramare.fxml"));
Parent root = (Parent) loader.load();
AddProgramareController AddPr=loader.getController();
AddPr.MyUsername(Numeutilzator.getText());
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Here is the class that adds the apointment to the db, I've pointed out whre the function for the username is, in the code below (I've added all the code so you can understand better what's going on)
package LicentaApp;
public class AddProgramareController implements Initializable {
ObservableList Timestamp=FXCollections.observableArrayList();
#FXML
private TextField Nume;
#FXML
private TextField Prenume;
#FXML
private TextField Ora;
#FXML
private DatePicker Data;
#FXML
private TextField Departament;
#FXML
private TextField Doctor;
#FXML
private TextField Nr_telefon;
#FXML
private TextField Numeutilizator;
public void initialize(URL location, ResourceBundle resources) {
}
*This is the function that I'll call in GetUsername from LogareController
public void MyUsername(String Numeutilizator) {
this.Numeutilizator.setText(Numeutilizator);
}
#FXML
private void AddProgramare(ActionEvent event) throws SQLException, IOException {
String Interogare1= "INSERT INTO programaritest(Nume,Prenume,Data,Ora,Departament,Doctor,Nr_telefon,Numeutilizator) VALUES(?,?,?,?,?,?,?,?)";
String nume=Nume.getText();
String prenume=Prenume.getText();
LocalDate data=Data.getValue();
String ora=Ora.getText();
String departament=Departament.getText();
String doctor=Doctor.getText();
String nr_telefon=Nr_telefon.getText();
String numeutilizator=Numeutilizator.getText();
try {
ConectaredB ConectaredB=new ConectaredB();
Connection conexiune=ConectaredB.logareDB();
PreparedStatement MG = conexiune.prepareStatement(Interogare1);
MG.setString(1, nume);
MG.setString(2, prenume);
MG.setDate(3, Date.valueOf(data));
MG.setString(4, ora);
MG.setString(5, departament);
MG.setString(6, doctor);
MG.setString(7, nr_telefon);
MG.setString(8, numeutilizator);
MG.executeUpdate();
// ...
} catch (SQLException exceptie1) {
exceptie1.printStackTrace();
}
}
}
The error message that I am getting when trying to add a new appointment is:
Caused by: java.lang.NullPointerException
at LicentaApp.AddProgramareController.AddProgramare(AddProgramareController.java:103)
At line 103: String numeutilizator=Numeutilizator.getText();
So the data from numeutilizator is not what the input is putting out, that means i failed passing the parameter from a controller to another.
P.S I have followed this Passing Parameters JavaFX FXML, couldn't figure it out.
There are multiple reasons this exception could happen:
The controller instance is not used with a fxml
the fxml the controller instance is used with does not contain a element with fx:id="Numeutilizator"
Therefore I'll post a simple example that gets a single string from a scene designed using fxml:
Application class
#Override
public void start(Stage primaryStage) {
Button btn = new Button("Get input");
btn.setOnAction((ActionEvent event) -> {
FXMLLoader loader = new FXMLLoader(getClass().getResource("dialog.fxml"));
Parent p;
try {
p = loader.load();
} catch (IOException ex) {
throw new RuntimeException(ex);
}
DialogController controller = loader.getController();
Stage stage = new Stage(StageStyle.UTILITY);
stage.initOwner(primaryStage);
stage.setScene(new Scene(p, 100, 100));
stage.showAndWait(); // display window and wait for close
if (controller.isCanceled()) {
System.out.println("canceled");
} else {
System.out.println("input submitted: " + controller.getText());
}
});
StackPane root = new StackPane(btn);
Scene scene = new Scene(root, 300, 300);
primaryStage.setScene(scene);
primaryStage.show();
}
dialog.fxml
<VBox xmlns:fx="http://javafx.com/fxml/1" fx:controller="fxml.DialogController">
<children>
<TextField fx:id="input"/>
<Button text="submit" onAction="#submit"/>
<Button text="cancel" onAction="#cancel"/>
</children>
</VBox>
fxml.DialogController
public class DialogController {
#FXML
private TextField input;
private boolean canceled = true;
public boolean isCanceled() {
return canceled;
}
public final String getText() {
return input.getText();
}
private void close(boolean canceled) {
this.canceled = canceled;
input.getScene().getWindow().hide();
}
#FXML
private void submit() {
close(false);
}
#FXML
private void cancel() {
close(true);
}
}
Instead of retrieving the info you could also pass a object containing logic to handle certain events to the controller before showing the fxml's content.
Related
a view from my work
public class LoginController implements Initializable {
Librarian l1=new Librarian();
#FXML
private TextField username;
#FXML
private PasswordField password;
#FXML
private Button loginButton;
#FXML
private Label status;
#FXML
private Label loginStatus;
#FXML
public void LibrarianLogin(ActionEvent event) {
try {
if(this.l1.isLogin(this.username.getText(), this.password.getText())) {
Stage stage = (Stage) this.loginButton.getScene().getWindow();
stage.close();
adminLogin();
}
else {
loginStatus.setText("Invalid");
}
}
catch(Exception locaException) {
}
}
public void adminLogin()
{
try {
Stage adminstage = new Stage();
FXMLLoader adminLoader = new FXMLLoader();
Pane adminroot = (Pane)adminLoader.load(getClass().getResource("/Librarian/admin.fxml").openStream());
AdminController adminController = (AdminController)adminLoader.getController();
Scene scene =new Scene(adminroot);
adminstage.setScene(scene);
adminstage.setTitle("Admin Dashboard");
adminstage.show();
}
catch (IOException e) {
e.printStackTrace();
}
}
#Override
public void initialize(URL location, ResourceBundle resources) {
if(this.l1.isDatabaseConnected()){
this.status.setText("Connected to Database");
}
else{
this.status.setText("Not Connected");
}
}
Connection connection;
public Librarian(){
try {
this.connection=dbConnection.getConnection();
}
catch (SQLException ex){
ex.printStackTrace();
}
if(this.connection == null){
System.exit(1);
}
}
public boolean isDatabaseConnected(){
return this.connection != null;
}
public boolean isLogin(String username,String id)throws Exception {
PreparedStatement pr= null;
ResultSet rs= null;
String sql = "SELECT * FROM Librarian where username = ? and password = ?";
try {
pr=connection.prepareStatement(sql);
pr.setString(1,username);
pr.setString(2, id);
rs = pr.executeQuery();
}
catch (SQLException ex) {
return false;
}
finally {
pr.close();
rs.close();
}
}
return false;
}
when ever i retrieve data from database to text box in login form nothing happens when i click the login button. Even when i enter wrong credentials the label to set login invalid doesn't work.
I made different packages:
package for logging in
package for database
package for admins after logging in
Thanks in advance :)
I have been working on JavaFX and trying figure out how to connect classes contained withing the package. I want the "text1btn" button from MainController class to send a text from "scene1TextField" also in MainController class to TextArea in LeftTextArea class. I would appreciate any comments on that. Thank you.
package sample;
public class Main extends Application {
public static BorderPane root = new BorderPane();
public static BorderPane getRoot() {
return root;
}
#Override
public void start(Stage primaryStage) throws Exception {
URL url1 = getClass().getResource("../view/MainView.fxml");
BorderPane bp1 = FXMLLoader.load(url1);
URL url2 = getClass().getResource("../view/LeftTextArea.fxml");
AnchorPane bp2 = FXMLLoader.load(url2);
root.setTop(bp1);
root.setCenter(bp2);
primaryStage.setScene(new Scene(root, 500, 400));
primaryStage.setResizable(false);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
package Controller;
public class MainController {
#FXML
Button scene1btn;
#FXML
Button scene2btn;
#FXML
TextField scene1TextField;
#FXML
TextField scene2TextField;
#FXML
Button text1btn;
#FXML
Button text2btn;
#FXML
TextArea mainViewTextArea;
#FXML
public void initialize() {
}
#FXML
public void text1btnClicked() {
}
#FXML
public void text2btnClicked() {
}
#FXML
private void scene1btnClicked() {
try {
URL url1 = getClass().getResource("../view/LeftTextArea.fxml");
AnchorPane bp1 = FXMLLoader.load(url1);
BorderPane border = Main.getRoot();
border.setCenter(bp1);
} catch (IOException e) {
e.printStackTrace();
}
}
#FXML
private void scene2btnClicked() {
try {
URL url2 = getClass().getResource("../view/RightTextArea.fxml");
AnchorPane bp2 = FXMLLoader.load(url2);
BorderPane border = Main.getRoot();
border.setCenter(bp2);
} catch (IOException e) {
e.printStackTrace();
}
}
}
package Controller;
public class LeftTextArea {
#FXML
public TextArea leftTextArea;
}
A quick and simple approach is just to expose a StringProperty in the MainController, and when it changes call a method in the LeftTextArea:
public class MainController {
private final StringProperty text = new SimpleStringProperty();
public StringProperty textProperty() {
return text ;
}
// existing code ...
#FXML
public void text1btnClicked() {
textProperty().set(scene1TextField.getText());
}
// ...
}
In LeftTextArea do
public class LeftTextArea {
#FXML
public TextArea leftTextArea;
public void setText(String text) {
leftTextArea.setText(text);
}
}
And then you can tie it all together with
#Override
public void start(Stage primaryStage) throws Exception {
URL url1 = getClass().getResource("../view/MainView.fxml");
FXMLLoader loader1 = new FXMLLoader(url1);
BorderPane bp1 = loader1.load();
MainController mainController = loader1.getController();
URL url2 = getClass().getResource("../view/LeftTextArea.fxml");
FXMLLoader loader2 = new FXMLLoader(url2);
AnchorPane bp2 = loader2.load();
LeftTextArea leftTextArea = loader2.getController();
mainController.textProperty().addListener((obs, oldText, newText) ->
leftTextArea.setText(newText));
root.setTop(bp1);
root.setCenter(bp2);
primaryStage.setScene(new Scene(root, 500, 400));
primaryStage.setResizable(false);
primaryStage.show();
}
If you end up needing multiple properties like this that are essentially shared between controllers, you probably need to define a "model" class to encapsulate them all in one place, and pass the model to the controllers. See, e.g. JavaFX controller to controller - access to UI Controls or Applying MVC With JavaFx
If you want to set any field in the class LeftTextArea just simply create a public setter method in Class LeftTextArea like
public void setTextArea(Text text){
//do what you want to do
}
Then call the method from MainController class with the object of LeftTextArea class. like
LeftTextArea leftTextArea = new LeftTextArea();
leftTextArea.setTextArea(text); //text is the desired you want to send
I'm new to JavaFX and I've been at this code for about 8 hours now and I've become a bit delusional with the code. My two main problems are:
Can't add new items to the TableView using my popUp box display().
Feels messy and unorganized. Any tips for better communication between FXML and Controllers? (Again I'm new so it could be that I've stared too long at it)
My main class
public class Main extends Application {
public static Stage primaryStage;
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage window) throws Exception {
try {
primaryStage = new Stage();
window = primaryStage;
Parent root = FXMLLoader.load(getClass().getResource("Fissto.fxml"));
Scene scene = new Scene(root);
window.setTitle("Fissto - the File Storage App!");
window.setScene(scene);
window.show();
}catch(Exception e){
e.printStackTrace();
}
// C.setLibraryStage();
}
}
My main Controller class (I have two sub ones that connect in the Fissto.fxml)
public class Controller implements Initializable{
Main main;
#FXML LibraryController libraryController = new LibraryController();
#FXML MergePageController mergePageController = new MergePageController();
private AddImageController addImageController = new AddImageController();
#FXML public void initialize(URL location, ResourceBundle resources){
System.out.println("View is now loaded!");
main = new Main();
libraryController.init(this);
mergePageController.init(this);
addImageController.init(this);
}
//Interface Initialization
public void setMergeStage() throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("Controllers/MergePage.fxml"));
Scene scene = new Scene(root);
main.primaryStage.setScene(scene);
}
public void setLibraryStage() throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("Controllers/LibraryPage.fxml"));
Scene scene = new Scene(root);
main.primaryStage.setScene(scene);
}
//Closing a window
public void closeWindow(){
main.primaryStage.close();
}
}
And finally the controller for the page that holds the TableView
public class LibraryController {
private Controller main;
//Library TableView Controllers
#FXML public TableView<Image> library;
#FXML private TableColumn<Image, String> NameColumn = new TableColumn<>();
#FXML private TableColumn<Image, ArrayList<String>> TagsColumn = new TableColumn<>();
#FXML private TableColumn<Image, String> CommentsColumn = new TableColumn<>();
#FXML private TableColumn<Image, String> FileLocationColumn = new TableColumn<>();
#FXML private TableColumn<Image, Integer> PointsColumn = new TableColumn<>();
public void init(Controller main){
System.out.println("LibraryPage Loading");
this.main = main;
addDataToColumns();
library = new TableView<>();
library.getItems().setAll(getImages());
System.out.println("LibraryPage Loaded");
}
//Initializes the column titles
private void addDataToColumns(){
NameColumn.setCellValueFactory(new PropertyValueFactory<>("name"));
TagsColumn.setCellValueFactory(new PropertyValueFactory<>("tags")); //TODO Convert to String format
CommentsColumn.setCellValueFactory(new PropertyValueFactory<>("comments"));
FileLocationColumn.setCellValueFactory(new PropertyValueFactory<>("filelocation"));
PointsColumn.setCellValueFactory(new PropertyValueFactory<>("points"));
}
//Gets all of the images
private ObservableList<Image> getImages() {
//TODO: Add where to actually get the data from
ObservableList<Image> images = FXCollections.observableArrayList();
String s = "Dog, Cat, Jumping Jack,";
ArrayList<String> list = Image.getTagOrganizer(',', s);
images.add(new Image("Test", list, "Comment", "No File Location, yet!", 10));
String k = "Calculus, Complex Numbers, Hard dude,";
ArrayList<String> list2 = Image.getTagOrganizer(',', k);
images.add(new Image("Number2", list2, "I love MathClub", "No File Location, yet!", -10));
return images;
}
This last class is the popup menu that takes in input to put in the GridPane
public class AddImageController {
private Controller main;
public void init(Controller main){
System.out.println("ImagePage Loaded");
this.main = main;
}
//Submitting an image to the library from the AddImagePage
public TextField nameInput;
public TextField tagsInput;
public TextField commentInput;
public TextField pointsInput;
public Label errorMessage;
/** TODO: Make it so that it writes to file then theoretically, the main controller should read from file every so often
* Main functionality for adding the information from the form to the database */
public void submitImage(){
if(!(nameInput.getText().trim().isEmpty()) && !(tagsInput.getText().trim().isEmpty()) && !(pointsInput.getText().trim().isEmpty())) {
if (isInt(pointsInput)) {
// System.out.print("Sent to database, *whoosh!*");
LibraryController c = new LibraryController();
ArrayList<String> s = Image.getTagOrganizer(',', tagsInput.getText());
Image image = new Image(nameInput.getText(), s, commentInput.getText(),"Location Needed", Integer.parseInt(pointsInput.getText()));
c.library.getItems().add(image);
clearInputs();
}
}else {
errorMessage.setText("Fill every field");
}
}
//Clears the input fields in the AddImagePage
public void clearInputs(){
nameInput.clear();
tagsInput.clear();
commentInput.clear();
pointsInput.clear();
errorMessage.setText("");
}
//Submission format verifiers
private boolean isInt(TextField input){
try{
int i = Integer.parseInt(input.getText());
errorMessage.setText("");
return true;
}catch (NumberFormatException e){
System.out.println("Oh no: " + input.getText() + " is not an integer");
errorMessage.setText("Points must be a number");
return false;
}
}
//Image Selection Handler
public void imageSelectionHandler(){
}
}
I understand it may be hard to read, so any feedback on how to make it easier to read in the future is much appreciated.
How can i add an item to an already existing ListView from a different Stage (Window).
Basically i just want to add Text to the ListView from Window 2 to the ListView in Window 1.
Thanks in advance.
Sorry i forgot this is what i've got so far in my controller class: (im a beginner in javafx ..)
(below i try to add a String to the ListView but this doesnt work...and i dont know why )
public class ClientGUIController implements Initializable {
#FXML private Text usernameText;
#FXML private Button cancelButtonNewDate;
#FXML private TextField newDateTitel,newDateJahr;
#FXML private TextArea newDateNotiz;
#FXML private ComboBox<String> newDateTag,newDateMonat,newDateStunde,newDateMinute;
#FXML private ListView<String> terminListView;
private ObservableList<String> termine =
FXCollections.observableArrayList();
private ObservableList<String> listItems = FXCollections.observableArrayList("Add Items here");
#Override
public void initialize(URL location, ResourceBundle resources) {
}
public Text getUsernameText() {
return usernameText;
}
public void setUsernameText(String username ) {
this.usernameText.setText(username);
terminListView.setItems(listItems);
listItems.add("test");
}
public void newDate() {
Stage newDate = new Stage();
Parent root;
try {
root = FXMLLoader.load(getClass().getResource("newDate.fxml"));
// FXMLLoader loader = new FXMLLoader();
// root = (Parent) loader.load(getClass().getResource("NewDate.fxml").openStream());
} catch (IOException e) {
e.printStackTrace();
return;
}
Scene sceneNewDate = new Scene(root);
// sceneNewDate.getStylesheets().add(getClass().getResource("Style.css").toExternalForm());
newDate.setTitle("Neuer Termin");
newDate.setScene(sceneNewDate);
newDate.show();
}
public void createNewDate() throws IOException {
// Termine meinTermin = new Termine(Integer.parseInt(newDateTag.getValue()), Integer.parseInt(newDateMonat.getValue()), Integer.parseInt(newDateJahr.getText()), newDateTitel.getText(), newDateNotiz.getText(),
// Integer.parseInt(newDateStunde.getValue()), Integer.parseInt(newDateMinute.getValue()));
//Add item to ListView
listItems.add("test"); <- this doesnt work
}
public void closeDialogue(){
Stage stage = (Stage) cancelButtonNewDate.getScene().getWindow();
stage.close();
}
}
One way to do this is to pass listItems to the controller for newDate.fxml, so it can just add to that list. So, assuming the controller class for newDate.fxml is NewDateController, you would do something like:
public class NewDateController {
private ObservableList<String> data ;
public void setData(ObservableList<String> data) {
this.data = data ;
}
// other code as before...
// button handler:
#FXML
private void handleButtonPress() {
data.addItem("test");
}
}
Then in your ClientGUIController, load the fxml like this:
public void newDate() {
Stage newDate = new Stage();
Parent root;
try {
FXMLLoader loader = new FXMLLoader(getClass().getResource("newDate.fxml"));
root = loader.load();
NewDateController controller = loader.getController();
controller.setData(listItems);
} catch (IOException e) {
e.printStackTrace();
return;
}
Scene sceneNewDate = new Scene(root);
newDate.setTitle("Neuer Termin");
newDate.setScene(sceneNewDate);
newDate.show();
}
Working with SceneBuilder. I have 2 stages, each one with a controller: stage1Controller,stage2Controller.
Stage1Controller :
public class Stage1Controller {
#FXML
private MenuItem translate;
#FXML
private Menu file;
#FXML
private Menu edit;
#FXML
private Menu help;
#FXML
private void handleTranslate (ActionEvent event){
translateFirstStage();
//HOW TO ACCESS THE stage2Controller setLabel()??
}
private void translateFirstStage(){
file.setText("Fichier");
edit.setText("Modifier");
help.setText("Aide");
}
}
Stage2Controller:
public class Stage2Controller {
#FXML
private Label lb;
private void setLabel(String string){
lb.setText("string");
}
}
Here is how both fxml files are loaded in Main.java class using 2 methods (called in Start(Stage primaryStage) method):
public void firstStage() {
try {
// Load root layout from fxml file.
FXMLLoader loader = new FXMLLoader();
loader.setLocation(Main.class.getResource("view/stage1.fxml"));
rootLayout = (BorderPane) loader.load();
// Show the scene containing the root layout.
Scene scene = new Scene(rootLayout);
primaryStage.setScene(scene);
primaryStage.show();
} catch (IOException e) {
e.printStackTrace();
}
}
public void secondStage() {
try {
// Load root layout from fxml file.
FXMLLoader loader = new FXMLLoader();
loader.setLocation(Main.class.getResource("view/stage2.fxml"));
XD = (AnchorPane) loader.load();
// Show the scene containing the root layout.
Scene scene = new Scene(XD);
Stage stage = new Stage();
stage.setScene(scene);
stage.show();
} catch (IOException e) {
e.printStackTrace();
}
}
The handleTranslate(ActionEvent event) method is used as an OnAction method for the MenuItem translate in the first Stage, it translates the view in both stages.
How Can i put setLabel in handleTranslate Method ? Thanks
The "quick and dirty" way is to give the Stage1Controller a reference to the Stage2Controller:
public class Stage1Controller {
private final Stage2Controller stage2Controller ;
public void setStage2Controller(Stage2Controller stage2Controller) {
this.stage2Controller = stage2Controller ;
}
// ...
#FXML
private void handleTranslate (ActionEvent event){
translateFirstStage();
stage2Controller.setLabel(...);
}
// other code as before ...
}
Now in your main app:
public class MainApp extends Application {
private Stage1Controller stage1Controller ;
private Stage2Controller stage2Controller ;
#Override
public void start(Stage primaryStage) {
firstStage();
secondStage();
stage1Controller.setStage2Controller(stage2Controller);
// ...
}
public void firstStage() {
try {
// Load root layout from fxml file.
FXMLLoader loader = new FXMLLoader();
loader.setLocation(Main.class.getResource("view/stage1.fxml"));
rootLayout = (BorderPane) loader.load();
stage1Controller = loader.getController();
// Show the scene containing the root layout.
Scene scene = new Scene(rootLayout);
primaryStage.setScene(scene);
primaryStage.show();
} catch (IOException e) {
e.printStackTrace();
}
}
public void secondStage() {
try {
// Load root layout from fxml file.
FXMLLoader loader = new FXMLLoader();
loader.setLocation(Main.class.getResource("view/stage2.fxml"));
XD = (AnchorPane) loader.load();
stage2Controller = loader.getController();
// Show the scene containing the root layout.
Scene scene = new Scene(XD);
Stage stage = new Stage();
stage.setScene(scene);
stage.show();
} catch (IOException e) {
e.printStackTrace();
}
}
// ...
}
A more robust approach to this, though, is to let both controllers access a shared model class, storing the data. If you represent the data using JavaFX observable properties, the controllers can listen for changes on the properties they care about. For example:
public class Model {
private final StringProperty text = new SimpleStringProperty("Initial text...");
public StringProperty textProperty() {
return text ;
}
public final void setText(String text) {
textProperty().set(text);
}
public final String getText() {
return textProperty().get();
}
// other properties as needed...
}
Now your controllers will look like this:
public class Stage1Controller {
private Model model ;
public void setModel(Model model) {
this.model = model ;
}
#FXML
private void handleTranslate (ActionEvent event){
translateFirstStage();
model.setText(...); // value will appear in stage2 controller's label!
}
// ...
}
and
public class Stage2Controller {
#FXML
private Label lb ;
private Model model ;
public void setModel(Model model) {
lb.textProperty().unbind();
this.model = model ;
lb.textProperty().bind(model.textProperty());
}
// ...
}
And in this case your main app looks like:
public class MainApp extends Application {
private final Model = new Model();
#Override
public void start(Stage primaryStage) {
// ...
}
public void firstStage() {
try {
// Load root layout from fxml file.
FXMLLoader loader = new FXMLLoader();
loader.setLocation(Main.class.getResource("view/stage1.fxml"));
rootLayout = (BorderPane) loader.load();
Stage1Controller controller = loader.getController();
controller.setModel(model);
// Show the scene containing the root layout.
Scene scene = new Scene(rootLayout);
primaryStage.setScene(scene);
primaryStage.show();
} catch (IOException e) {
e.printStackTrace();
}
}
public void secondStage() {
try {
// Load root layout from fxml file.
FXMLLoader loader = new FXMLLoader();
loader.setLocation(Main.class.getResource("view/stage2.fxml"));
XD = (AnchorPane) loader.load();
Stage2Controller controller = loader.getController();
controller.setModel(model);
// Show the scene containing the root layout.
Scene scene = new Scene(XD);
Stage stage = new Stage();
stage.setScene(scene);
stage.show();
} catch (IOException e) {
e.printStackTrace();
}
}
}