Search code examples
javadatabaseoracle-databaseswingconnection

My linkage from a JFrame to another Jframe does not work


I'd like to create a login menu. After logged, there should appears a new JFrame. All JFrames have been created using Netbeans' GUI Builder (Design). For some reason, the linkage from the former JFrame to the second does not work. Indeed, after i press "Entra" on the former JFrame, it only disposes and does not appears the second Form. This is the "First" Form, which contains the login menu

import java.awt.event.KeyEvent;
import java.sql.SQLException;


public class Login extends javax.swing.JFrame {

    boolean premutoLogin;

    //some stuff automatically generated...                                               

    private void entraButtonActionPerformed(java.awt.event.ActionEvent evt) {                                            
        eseguiLogin();
    }                                           


    private void eseguiLogin(){
        Database.schema = Database.user = usernameTextField.getText();
        Database.password = new String(passwordField.getPassword());
        try{
            Database.setDefaultConnection(Database.connetti());
            premutoLogin = true;
            dispose();
        } catch(SQLException exc){
            PrincipaleCF.mostraErroriSwing(this, exc);
        }
    }


}

The former JFrame should link to this JFrame:

import java.awt.Color;
import java.sql.*;
import javax.swing.JOptionPane;

public class PrincipaleCF extends javax.swing.JFrame {
    /**
     * Creates new form PrincipaleCF
     */
    public PrincipaleCF() {
        initComponents();
        Login login = new Login(this, true);
        login.setVisible(true);
        if(!login.premutoLogin)
            dispose();
        else
            mostraDefault();            
    }

    private void mostraDefault(){
        setVisible(true);
        this.getRootPane().setDefaultButton(checkButton);
    }

    //edit by the gui builder          

    private void esciButtonActionPerformed(java.awt.event.ActionEvent evt) {                                           
        try{
            Database.getDefaultConnection().close();
        } catch(SQLException exc){
            mostraErroriSwing(this, exc);
        }
        dispose();
    }                                          

    private void checkButtonActionPerformed(java.awt.event.ActionEvent evt) {                                            
        eseguiControllo();
    }                                           


    public static void mostraErroriSwing(java.awt.Component thrower, SQLException e){
        //my definition...
    }

    //psv main generated automatically

        /* Create and display the form */
       //...


}

For some reason, it compiles and run with no problem. But when i click "Entra" on the former form, it only disposes but does not link to the second Frame.


Solution

  • For starters, I would advise you to import your classes instead of having things like

    public class PrincipaleCF extends javax.swing.JFrame {

    but

    import javax.swing.JFrame;
    
    public class PrincipaleCF extends JFrame {
    

    To resolve your problem of frame linkage, you just need to call a function that will open your second window, as it closes the first one, that would be, something like

    private void eseguiLogin(){
        Database.schema = Database.user = usernameTextField.getText();
        Database.password = new String(passwordField.getPassword());
        try{
            Database.setDefaultConnection(Database.connetti());
            premutoLogin = true;
            dispose();
    
            //this
            PrincipaleCF secondFrame = new PrincipaleCF();
        } catch(SQLException exc){
            PrincipaleCF.mostraErroriSwing(this, exc);
        }
    }