Solved

Problem with multiple GUI's popping up when inputting info

Posted on 2004-08-10
18
355 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
ID: 11761088
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
ID: 11761094
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
ID: 11761111
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
ID: 11761116
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
ID: 11761120
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
ID: 11761121
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
ID: 11761122
yeah, as I said ;-)
0
 
LVL 35

Expert Comment

by:TimYates
ID: 11761123
>  yeah, as I said ;-)

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

Expert Comment

by:JugglerW
ID: 11761124
Oh just saw that you create the loginGUI at two places:

In your main method
and
in your login class again
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 7

Expert Comment

by:JugglerW
ID: 11761129
Oooops too late :-)
0
 
LVL 86

Expert Comment

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

Expert Comment

by:TimYates
ID: 11761156
CEHJ is right too ;-)
0
 
LVL 92

Expert Comment

by:objects
ID: 11761265
> CEHJ is right too ;-)

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

Expert Comment

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

It still needs to be done.

0
 

Author Comment

by:cutjack
ID: 11762217
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
ID: 11762243
Move the database logic somewhere else then.
0
 
LVL 35

Accepted Solution

by:
TimYates earned 300 total points
ID: 11762270
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
ID: 11762278
> 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Unable to start eclipse ? 17 134
bitbucket vs gitbucket 3 58
Problem to start Neon 20 89
printing a file in reverse order is easy in recursion rather than in iteration 3 51
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

862 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

30 Experts available now in Live!

Get 1:1 Help Now