Can someone suggest ,how can we pass the value from one controller to another controller . My scenario is below
I need to get the username on my home page after login. please share the piece of code.
Thanks in Advance!!!
Can someone suggest ,how can we pass the value from one controller to another controller . My scenario is below
I need to get the username on my home page after login. please share the piece of code.
Thanks in Advance!!!
As I understood from your question, you need a login screen and then other screens in a row for navigation. First user has to sign in and after validating then the user information will go to the Home screen. Below is the codes which may help you -
Main.java
package sample;
import javafx.application.Application;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
LoginController loginController = new LoginController();
loginController.launchLogingController(primaryStage);
}
public static void main(String[] args) {
launch(args);
}
}
LoginController.java
package sample;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import java.io.IOException;
public class LoginController {
private Parent parent;
private Scene scene;
private Stage stage;
@FXML
private TextField userName;
@FXML
private TextField passwordField;
private HomeController homeController;
public LoginController() {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/fxml/login.fxml"));
fxmlLoader.setController(this);
try {
parent = (Parent) fxmlLoader.load();
scene = new Scene(parent, 600, 400);
} catch (IOException e) {
e.printStackTrace();
}
}
@FXML
protected void handleSubmitButtonAction(ActionEvent event) {
System.out.println(userName.getText());
if (userName.getText().trim().length() > 0 && passwordField.getText().trim().length() > 0) {
homeController = new HomeController();
homeController.redirectHome(stage, userName.getText().trim());
}
}
public void launchLogingController(Stage stage) {
this.stage = stage;
stage.setTitle("User Login");
stage.setScene(scene);
stage.setResizable(true);
stage.hide();
stage.show();
}
}
HomeController.java
package sample;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import java.io.IOException;
public class HomeController {
private Parent parent;
private Scene scene;
private Stage stage;
@FXML
private Text welcomeText;
public HomeController() {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/fxml/home.fxml"));
fxmlLoader.setController(this);
try {
parent = (Parent) fxmlLoader.load();
scene = new Scene(parent, 600, 400);
} catch (IOException e) {
e.printStackTrace();
}
}
public void redirectHome(Stage stage, String name) {
stage.setTitle("Home");
stage.setScene(scene);
welcomeText.setText("Hello " + name + "! You are welcome.");
stage.hide();
stage.show();
}
}
home.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.net.*?>
<?import javafx.geometry.*?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.*?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.text.*?>
<?import sample.LoginController?>
<GridPane alignment="CENTER" hgap="10.0" vgap="10.0" xmlns:fx="http://javafx.com/fxml">
<padding>
<Insets bottom="10.0" left="25.0" right="25.0" top="25.0" />
</padding>
<Text fx:id="welcomeText" text="home" GridPane.columnIndex="0" GridPane.columnSpan="2" GridPane.rowIndex="0" />
</GridPane>
login.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.net.*?>
<?import javafx.geometry.*?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.*?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.text.*?>
<?import sample.LoginController?>
<GridPane alignment="CENTER" hgap="10.0" vgap="10.0" xmlns:fx="http://javafx.com/fxml">
<padding>
<Insets bottom="10.0" left="25.0" right="25.0" top="25.0" />
</padding>
<Text text="Login" GridPane.columnIndex="0" GridPane.columnSpan="2" GridPane.rowIndex="0" />
<Label text="User Name:" GridPane.columnIndex="0" GridPane.rowIndex="1" />
<TextField fx:id="userName" GridPane.columnIndex="1" GridPane.rowIndex="1" />
<Label text="Password:" GridPane.columnIndex="0" GridPane.rowIndex="2" />
<PasswordField fx:id="passwordField" GridPane.columnIndex="1" GridPane.rowIndex="2" />
<HBox alignment="BOTTOM_RIGHT" spacing="10.0" GridPane.columnIndex="1" GridPane.rowIndex="4">
<Button onAction="#handleSubmitButtonAction" text="Sign In" />
</HBox>
</GridPane>
Using Maven will be better for tackling problems.
Thanks!