-2

I am making a login system using javaFX. when the user enters correct username and password I want to redirect him to the "home.fxml" How can I do that, currently I am printing a message when the user enter correct data. I can seperatly create a method to link to a new scene, but when I call it in the if statement It is asking for the parameters.

package sample;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import javafx.event.ActionEvent;

import java.io.IOException;
import java.net.URL;
import java.sql.*;
import java.util.ResourceBundle;

public class Controller {
    @FXML
    private Button cancelbutton;
    @FXML
    private Label loginmessagelabel;
    @FXML
    private PasswordField password1;
    @FXML
    private TextField username1;
    PreparedStatement pst;
    ResultSet rs;
    @FXML
    private Stage stage;
    private Scene scene;
    private Parent root;


    public void validatelogin() throws SQLException, ClassNotFoundException, IOException {
        String jdbcURL = "jdbc:mysql://localhost/medibase";
        String username = "root";
        String password = "0852";
        Connection connection = DriverManager.getConnection(jdbcURL,username,password);
        Class.forName("com.mysql.jdbc.Driver");
        String uname = username1.getText();
        String psd = password1.getText();
        pst=connection.prepareStatement("SELECT * FROM user_account WHERE username=? and password=?");
        pst.setString(1, uname);
        pst.setString(2, psd);
        rs = pst.executeQuery();
        if(rs.next()){
        loginmessagelabel.setText("Congratulations");
   


           

        } else{
            loginmessagelabel.setText("Login failed");
        }

    }

I can create a method like bellow , but when I call this in the if statement It is asking for parameters

public void switchtoHome(ActionEvent event)throws IOException {
    Parent root = FXMLLoader.load(getClass().getResource("home.fxml"));
    stage = (Stage)((Node)event.getSource()).getScene().getWindow();
    scene=new Scene(root);
    stage.setScene(scene);
    stage.show();
}
Banchi Secret
  • 21
  • 1
  • 1
  • 3
  • 2
    [mcve] please .. – kleopatra Jul 14 '21 at 10:31
  • See comments below https://stackoverflow.com/questions/68362193/how-can-switch-between-scenes-if-the-condition-is-true-javafx which answer this question. – James_D Jul 14 '21 at 13:31
  • Look at `LoginScreenController` [here](https://github.com/sedj601/SimpleLoginFx/tree/master/src/simpleloginfx). Hopefully, it can be of some help. – SedJ601 Jul 14 '21 at 15:19

1 Answers1

0

See my answer on this Question about remembering the logged user

Specifically on the changeToMainWindow() method, and call that method when loggin is successful.

ADSquared
  • 207
  • 3
  • 12