Pass a value form a form to another form in java

1.9k Views Asked by At

I want to pass stuId value from LoginForm to IndoorCategoriesForm, which comes next to Home and Categories forms after login.

I've tried overloading the method in IndoorCategoriesForm

String id;
//overload the constructor
public IndoorCategoriesForm(String stuId){
    initComponents();
    //get student id from the login 
    this.id = stuId
    labelStudentId.setText(id);
}

Then used the getText() method to get the value inside the table. When I run the project the Enrolled table in the database shows the value of the SportId but the StudentId is empty.

IndoorCategoris Form:

private void btnEnrollBasketBallActionPerformed(java.awt.event.ActionEvent evt) {                                                    
    PreparedStatement pst;
    
    //query to enrol the user 
    String enrollUserQuery = "INSERT INTO `Enrolled`(`StuId`, `SpId`) VALUES (?, ?)";
   
    //get student id from the login text field
    String stuId = labelStudentId.getText();
    
    //basketball sport id
    String basketball = "1002";       
    
    try {
            pst = DbConnection.getConnection().prepareStatement(enrollUserQuery);
            pst.setString(1, stuId);
            pst.setString(2, basketball);

            if (pst.executeUpdate() != 0){
                //if enrolling successfull, show enroll success form
                EnrollSuccessfullForm esf = new EnrollSuccessfullForm();
                esf.setVisible(true);
                esf.pack();
                esf.setLocationRelativeTo(null);
                esf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                this.dispose();
            }
            else{
                JOptionPane.showMessageDialog(null, "You have already enrolled");
            }
            
    } catch (SQLException ex){
        Logger.getLogger(IndoorCategoriesForm.class.getName()).log(Level.SEVERE, null, ex);
    }
}

Login Form:

    private void buttonLogInActionPerformed(java.awt.event.ActionEvent evt) {
    PreparedStatement pst;
    ResultSet rs;
    
    //get stu id and password
    String stuId = jTextFieldId.getText();
    String pass = String.valueOf(jPasswordField.getPassword());
    
    //check if the stuId exist in the database
    String userLoginQuery = "SELECT * FROM `Student` WHERE `Stu_Id` = ? AND `Stu_Password` = ?";
    
    
    if(stuId.trim().equals(""))
    {
        JOptionPane.showMessageDialog(null, "Please enter a user ID", "Empty Field", 2);
        
    }
    else if(pass.trim().equals("")) {
        JOptionPane.showMessageDialog(null, "Please enter a password", "Empty Field", 2);
    }
    else
    {
        try {
            pst = DbConnection.getConnection().prepareStatement(userLoginQuery);
            pst.setString(1, stuId);
            pst.setString(2, pass);
            rs = pst.executeQuery();
            
            if(rs.next()){
                //get value from the student Id to pass to Indoor categories form
                new IndoorCategoriesForm(stuId).setVisible(false);
                
                //shows the home page
                HomeForm hf = new HomeForm();
                hf.setVisible(true);
                hf.pack();
                hf.setLocationRelativeTo(null);
                hf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                this.dispose();
            }
            else {
                JOptionPane.showMessageDialog(null, "Invalid user Id or pasword", "Login Error", 2);
            }

        } catch (SQLException ex) {
            Logger.getLogger(LogInForm.class.getName()).log(Level.SEVERE, null, ex);
        }   
    }
}
1

There are 1 best solutions below

0
ADSquared On

Even if it's bad practice, you could have a public String variable in your Indoor frame,

public String passedId;

and when you want to pass the value from your Login form, you create an instance of your Indoor frame and then set the String variable passedId:

IndoorFrame iF = new IndoorFrame();
iF.passedId = pass your variable here

Just create an instance of the frame you want to pass data too, then set the String variable from that class to the value you want to pass to it.

In any case, like mentioned in the comments, multiple JFrames might not be good practice.

Good luck