Solved

Problem with multiple GUI's popping up when inputting info

Posted on 2004-08-10
18
354 Views
Last Modified: 2010-03-31
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
Comment
Question by:cutjack
  • 6
  • 4
  • 3
  • +4
18 Comments
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
You should separate out the login logic from the db access logic, and the latter should run in a separate thread
0
 
LVL 35

Expert Comment

by:TimYates
Comment Utility
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
 
LVL 7

Expert Comment

by:JugglerW
Comment Utility
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
 
LVL 9

Expert Comment

by:Nick_72
Comment Utility
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
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
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
 
LVL 35

Expert Comment

by:girionis
Comment Utility
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
 
LVL 35

Expert Comment

by:TimYates
Comment Utility
yeah, as I said ;-)
0
 
LVL 35

Expert Comment

by:TimYates
Comment Utility
>  yeah, as I said ;-)

Whoops...that was directed at Nick_72 ;-)  This question is going too quick ;-)
0
 
LVL 7

Expert Comment

by:JugglerW
Comment Utility
Oh just saw that you create the loginGUI at two places:

In your main method
and
in your login class again
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 7

Expert Comment

by:JugglerW
Comment Utility
Oooops too late :-)
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
Tim is right - but you should still reorganise the code as i mentioned, incorporating his suggestion
0
 
LVL 35

Expert Comment

by:TimYates
Comment Utility
CEHJ is right too ;-)
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
> CEHJ is right too ;-)

though its not actually related to the problem at hand ;)
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>though its not actually related to the problem at hand ;)

It still needs to be done.

0
 

Author Comment

by:cutjack
Comment Utility
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
 
LVL 35

Assisted Solution

by:girionis
girionis earned 200 total points
Comment Utility
Move the database logic somewhere else then.
0
 
LVL 35

Accepted Solution

by:
TimYates earned 300 total points
Comment Utility
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
 
LVL 35

Expert Comment

by:TimYates
Comment Utility
> 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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now