Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 365
  • Last Modified:

Problem with multiple GUI's popping up when inputting info

Hi,

Hope someone out there can help me with this, when I am programming an application and using GUI's I run the application and the GUI pops up as it should. I enter the login name and password, but the this does not gain me access to the database. Instead the same Login GUI pops up again but this time when I enter the data it allows me to continue into the application as it should. It seems like a minor problem but causing me some hassle so any help is appreciated.

P.S. Code causing the problem is below


THIS IS THE GUI CODE

public class loginGui extends javax.swing.JFrame {

    /** Creates new form loginGui */
    public loginGui() {
        initComponents();
    }

    private void initComponents() {//GEN-BEGIN:initComponents
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        usertxt = new javax.swing.JTextField();
        passtxt = new javax.swing.JPasswordField();
        logbut = new javax.swing.JButton();
        cancelbut = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();

        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        setTitle("Nebuliser Tracking Login Screen");
        setLocationRelativeTo(this);
        setMaximizedBounds(new java.awt.Rectangle(0, 0, 0, 0));
        setName("loginframe");
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                exitForm(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Bookman Old Style", 0, 18));
        jLabel1.setText("Nebuliser Tracking System");
        getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 40, -1, -1));

        jLabel2.setText("Username");
        getContentPane().add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 110, -1, -1));

        jLabel3.setText("Password");
        getContentPane().add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 140, -1, -1));

        getContentPane().add(usertxt, new org.netbeans.lib.awtextra.AbsoluteConstraints(170, 110, 100, -1));

        getContentPane().add(passtxt, new org.netbeans.lib.awtextra.AbsoluteConstraints(170, 140, 100, -1));

        logbut.setText("Login");
        logbut.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                logbutActionPerformed(evt);
            }
        });

        getContentPane().add(logbut, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 200, -1, -1));

        cancelbut.setText("Cancel");
        getContentPane().add(cancelbut, new org.netbeans.lib.awtextra.AbsoluteConstraints(210, 200, -1, -1));

        jLabel4.setText("Maintenance");
        getContentPane().add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(320, 280, -1, -1));

        pack();
    }//GEN-END:initComponents

    private void logbutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_logbutActionPerformed
       login l = new login();
       l.verifylogin();

    }

    private void exitForm(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_exitForm
        System.exit(0);
    }

    public static void main(String args[]) {
        new loginGui().show();
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    public javax.swing.JButton cancelbut;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    public javax.swing.JButton logbut;
    public javax.swing.JPasswordField passtxt;
    public javax.swing.JTextField usertxt;
    // End of variables declaration//GEN-END:variables

}

THIS IS THE CODE ITS CALLING WHEN I CLICK ON THE LOGIN BUTTON

public class login extends javax.swing.JFrame
{            
      static loginGui lg;      
      static addNewClient anc;      
      public login(){}
      
      public void verifylogin()
    {
            lg = new loginGui();
          lg.show();                  
        lg.logbut.addActionListener(new ActionListener()
        {
                   public void actionPerformed(ActionEvent e)
                       {
                             try{                              
                              try
                              {
                                    String user = lg.usertxt.getText();
                                    String password = lg.passtxt.getText();
                                    System.out.println("user = "+user+" Password = "+password);
                              //      System.out.println("user2 = "+lg.usertxt.getText()+" Password2 = "+lg.passtxt.getText());
                        
                        
                                    String sql = "SELECT * FROM login WHERE username='"+user+"'  AND password='"+password+"'";                              
                                    
                                    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                                    Connection conn = DriverManager.getConnection("jdbc:odbc:nebs");
                                    Statement stmt = conn.createStatement();                                    
                                    stmt.executeQuery(sql);
                                    ResultSet rs = stmt.getResultSet();
                  
                                    boolean recordfound = rs.next();
                              
                                    if (recordfound){
                                          anc = new addNewClient();
                                        anc.show();
                                          System.out.println("Record Found");                        
                                    }
                                    else{      
                                          JOptionPane.showMessageDialog(lg,"Username and Password do not match.",
                                   "Please Re-Enter.",JOptionPane.ERROR_MESSAGE/*.INFORMATION_MESSAGE*/);       
                                          System.out.println("Handicapped");
                              
                                    }
                  
                                    rs.close();
                                    stmt.close();
                                    conn.close();                  
                              }catch(Exception ex)
                              {
                              }
                        }
                        catch(Exception exe)
                        {}            
                  }
            });
      }
}      
0
cutjack
Asked:
cutjack
  • 6
  • 4
  • 3
  • +4
2 Solutions
 
CEHJCommented:
You should separate out the login logic from the db access logic, and the latter should run in a separate thread
0
 
TimYatesCommented:
in loginGui, clicking the button;

    private void logbutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_logbutActionPerformed
       login l = new login();
       l.verifylogin();

    }

creates a login object, and calls l.verifyLogin()

the first thing verifyLogin does is:

     public void verifylogin()
    {
          lg = new loginGui();
         lg.show();  

Which creates a login dialog again...

That's what's wrong ;-)

0
 
JugglerWCommented:
Print a stack trace in your exception catch clauses. Perhaps you get some idea what happened.

E.g. change:

                         }catch(Exception ex)
                         {
                         }

to

                         }catch(Exception ex)
                         {
                             ex.printStackTrace();
                         }

and

                    catch(Exception exe)
                    {}          

to

                    catch(Exception exe)
                    {
                           exe.printStackTrace();
                    }          
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Nick_72Commented:
Hi,

This is the main method:

public static void main(String args[]) {
        new loginGui().show();
}

you create a loginGui and show it,
but in the verifylogin method of the login class, you create another loginGui and show it:

public class login extends javax.swing.JFrame
{          
     static loginGui lg;    
     static addNewClient anc;    
     public login(){}
     
     public void verifylogin()
    {
          lg = new loginGui();  <---- here another one is created
         lg.show();

That's why you get multiple frames.

/Nick
0
 
CEHJCommented:
public void actionPerformed(ActionEvent e)
{
            String user = lg.usertxt.getText();
            String password = lg.passtxt.getText();
            runQuery(user, password0;
}


public void runQuery(String user, String password)
{

      Thread queryThread = new Thread(new Query(user, password));
      queryThread.start();
      
}


class Query implements Runnable
{
      public Query(String user, String password)
      {
            // assign to instance vars
      }
      
      public void run()
      {
            // run query
      }
}
0
 
girionisCommented:
In your login class you have a call to the logingui and in the logingui you call the login class. Why are you doing it?
0
 
TimYatesCommented:
yeah, as I said ;-)
0
 
TimYatesCommented:
>  yeah, as I said ;-)

Whoops...that was directed at Nick_72 ;-)  This question is going too quick ;-)
0
 
JugglerWCommented:
Oh just saw that you create the loginGUI at two places:

In your main method
and
in your login class again
0
 
JugglerWCommented:
Oooops too late :-)
0
 
CEHJCommented:
Tim is right - but you should still reorganise the code as i mentioned, incorporating his suggestion
0
 
TimYatesCommented:
CEHJ is right too ;-)
0
 
objectsCommented:
> CEHJ is right too ;-)

though its not actually related to the problem at hand ;)
0
 
CEHJCommented:
>>though its not actually related to the problem at hand ;)

It still needs to be done.

0
 
cutjackAuthor Commented:
I know ye are right in saying that I am calling the GUI in 2 places but I have to call it in the second place so as to access the database. If i don't place it here I just get the first GUI and when i enter the details, the screen just waits there, the database is never accessed. It doesn't seem to enter the listener until I enter the username and password in the GUI being called in verifylogin. I have to call it in both places before I can gain access.... Does anybody know why this is.
0
 
girionisCommented:
Move the database logic somewhere else then.
0
 
TimYatesCommented:
Because like CEHJ says, you have your logic all mixed up...

You seem to be running the dialog, which then calls the db access stuff, but because the db access stuff doesn't know about the dialog (and has it's own instance of it), it fires up another dialog to get the information...

You need to think again about what it is you are trying to do, and redesign your classes...  I can remommend going the way CEHJ suggests...

Also, why are the variables in your db class "static"?
0
 
TimYatesCommented:
> but I have to call it in the second place so as to access the database.

So you knew why you were getting two dialogs all along :-)
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 6
  • 4
  • 3
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now