CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Feb 2012
    Posts
    6

    Post Simple SQL Login to run the main class

    Hey -
    I am currently studying A level computing at sixth form -
    We are coding in java this year and to say the least - im crap.
    Im trying to run a gui from a main class. From this gui they will input there username and password, which will validate with the sql database (All working fine)
    However i need to work a way of getting the login class to give the main class the username of the person that was logged in.
    I can then run various things of there username - whether they are a admin or not, and there current test scores.

    However i cant seem to get the main class to run again when the login has been successful.
    I've tried while and if's and cant think what else will make it happen!

    Here is my code for the main:
    Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package computinggraphtransformations;
    
    /**
     *
     * @author Joey
     */
    public class Main {
    public static boolean logged = false;
    public static String mainUsername;
    public static boolean execLoop = true;
    
    //if (Logged == true){
    //}
    
        /**
         * @param args the command line arguments
         */
        public static void main(String args[]) {
            //String mainUsername=null;
            //String mainUsername = args[0];
            //System.out.println(args[0]);
            while(execLoop == true){
            
            
            System.out.println("sdfhskdjf");
            if(logged == false){
                NewsqlLogin newsql = new NewsqlLogin();
                System.out.println("logged = false");
                System.out.println(mainUsername);
                            execLoop = false;
    
            }else if(logged == true){
                System.out.println("logged = else");
                System.out.println("debug " + mainUsername);
                
                
            }
        }
        }
        
        public static void newmain() {    
            Main Main = new Main();
            
        }
        
    }

    And for the Login window
    Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package computinggraphtransformations;
    
    
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.UnsupportedEncodingException;
    import javax.swing.*;
    import java.security.MessageDigest;
    import java.security.NoSuchAlgorithmException;
    import java.sql.*;
    
    
    /**
     *
     * @author Joey
     */
    
    
    
    
    public final class NewsqlLogin extends JFrame{
        private boolean success = false;
        public static String username;
        //public static String password;
    
    
        ///**
         //* @param args the command line arguments
         //*/
        //public static void Main(String[] args) {
            // TODO code application logic here
            //new NewsqlLogin(); //Shows the GUI
        //}
        
        
       // public final class PasswordJDialog extends JDialog {
       //     private final JPanel backgroundPanel;
    
       // }
        
        
         public NewsqlLogin() {
            Components();
            this.pack();
            this.setLocationRelativeTo(null);
            this.setVisible(true);
        }
        
        private void Components() {
            Container c = getContentPane();
            
            //super "Login Window";
            
            //Create the Java components
            JLabel lblUsername = new JLabel("Username: ");
            lblUsername.setHorizontalAlignment(JLabel.CENTER);
            final JTextField txtUsername = new JTextField(10);
            JLabel lblPassword = new JLabel("Password: ");
            lblPassword.setHorizontalAlignment(JLabel.CENTER);
            final JPasswordField txtPassword = new JPasswordField(10);
            txtPassword.setEchoChar('*');
            JButton btnSubmit = new JButton("Submit");
            JButton btnCancel = new JButton("Cancel");
            //btnSubmit.setSize(300, 300);
            
            
            btnSubmit.addActionListener( new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    username = txtUsername.getText().trim();
                    String password = null;
                    
                        password = new String(txtPassword.getPassword()).trim();
                     
                        
                        
                    success = submit(username, password);
                    if(success) {
                        JOptionPane.showMessageDialog(null, "Successful login");
                        System.out.println("logged set to true");
                        String id[] = new String[1];
                        id[0]=username;     
                        Main.mainUsername=username;
                        Main.logged = true;
                        Main.main(id);
                        Main.execLoop = true;
                        System.out.println("Logged, Username, Exec Loop");
                        System.out.println(Main.logged);
                        System.out.println(Main.mainUsername);
                        System.out.println(Main.execLoop);
                        System.out.println("New main");
                        
                        new Main();
                        System.out.println("New values");
                        System.out.println(Main.logged);
                        System.out.println(Main.mainUsername);
                        System.out.println(Main.execLoop);
                        
                        
                        System.out.println("Main class started");
                        close();
                    } else {
                        JOptionPane.showMessageDialog(null, "Incorrect username or password");
                    }
                }
    
          
                    
                
            });
    
            btnCancel.addActionListener( new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    close();
                }
            });
            
            
            //Add the components to the JFrame
            JPanel cp = new JPanel();
            cp.setPreferredSize(new Dimension(300,100));
            cp.setOpaque(false);
            cp.setLayout(new GridLayout(3, 3, 5, 5));
            cp.add(lblUsername);
            //lblUsername.setSize(300, 300);
            cp.add(txtUsername);
            cp.add(lblPassword);
            cp.add(txtPassword);
            cp.add(btnSubmit);
            cp.add(btnCancel);
            
            c.add(cp) ;
    
          pack();
          setSize( 250, 150 );
          //show();
          
            
            
            
        }
        
        
         protected boolean submit(String username, String password) {
            if(username.length() < 1 || password.length() < 1)
                return false;
            
            return hashAndVerify(username, password);
        }
         
        private boolean hashAndVerify(String username, String password) {
    
    		String query = "SELECT Password FROM userDB WHERE Username = ?";
    		PreparedStatement stmt = DatabaseConnection.getStatement(query);
    		ResultSet rs = null;
    		try {
    			stmt.setString(1, username);
    			rs = stmt.executeQuery();
    			while (rs.next()) {
    				String arr = rs.getString(password);
    				if (password.equals(arr)) {
    					return true;
    				}
    			}
    		} catch (SQLException e1) {
    			e1.printStackTrace();
    		} finally {
    			try {
    				if (rs != null)
    					rs.close();
    				if (stmt != null)
    					stmt.close();
    				DatabaseConnection.closeConnection();
    			} catch (SQLException e) {
    				e.printStackTrace();
    			}
    		}
    
    		return false;
    	}
        
        
      /*  private byte[] hash(String password) {
    		MessageDigest sha1 = null;
    		try {
    			sha1 = MessageDigest.getInstance("SHA-1");
    
    		} catch (NoSuchAlgorithmException e) {
    			e.printStackTrace();
    		}
    
    		if (sha1 == null)
    			return null;
    
    		sha1.update(password);
    		return sha1.digest();
    	}*/
        
        
        
        protected void close() {
            this.dispose();
        }
        
        public static void NewsqlLogin() {    
            //new NewsqlLogin();
            
        }
        
    }


    Any help is much appreciated -

    I hope to get all the GUI's to run from the main class, and then the main class can work out what to do (mostly run the main menu) when it receives some sort of response from the other gui classes.

    Thanks

    Joe.


    P.S Have also attached the three .java's as tags dont seem to work
    Attached Files Attached Files

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Simple SQL Login to run the main class

    Welcome and well done for using code tags.

    Some general comments:
    • Java is case sensitive - I noticed in some of your commented out code you had used the wrong case.
    • Never check if boolean values equal true or false, it's ugly and unnecessary.
      if(logged == true) should be written as if(logged)
      if(logged == false) should be written as if(!logged)
    • This also highlights the use good names because your code should read properly.
      if(logged) doesn't really say what the code is testing whereas if(loggedIn) does.


    The reason your code is not looping is because you are displaying the login panel and then setting execLoop to false.

    Because the GUI works on the event dispatch thread you need to remove setVisible from the NewsqlLogin constructor and call it via the EventQueue's invokeAndWait() method. When the panel closes the you need to query the panel to see if the user logged in and take the appropriate action. ie

    Code:
        final NewsqlLogin loginPanel = new NewsqlLogin();
        EventQueue.invokeAndWait(new Runnable()
            {
                
                @Override
                public void run()
                    {
                    loginPanel.setVisible(true);
                    }
            });
    
        loggedIn = loginPanel.isLoggedIn();
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  3. #3
    Join Date
    Feb 2012
    Posts
    6

    Re: Simple SQL Login to run the main class

    Hey keang
    Thanks for you're reply and points - The boolean point is very helpful!
    Yes i need to try and name things more appropriately and comment my code however it seems such an effort at the time! haha

    Okay - I've tried a couple different things with the code you gave me, i was unsure where to put it however i guess in the NewsqlLogin.NewsqlLogin() class.

    I've added it as so
    Code:
    public NewsqlLogin() throws InterruptedException, InvocationTargetException {
            Components();
            this.pack();
            this.setLocationRelativeTo(null);
            
            final NewsqlLogin loginPanel = new NewsqlLogin();
        EventQueue.invokeAndWait(new Runnable()
            {
                
                @Override
                public void run()
                    {
                    loginPanel.setVisible(true);
                    }
            });
    
        Main.loggedIn = loginPanel.isLoggedIn();
            
        }
    And changed the main class (I've removed all the messy comments)
    [CODE]
    public class Main {
    public static boolean loggedIn = false;
    public static String mainUsername;
    public static boolean execLoop = true;

    public static void main(String args[]) {
    while(execLoop == true){
    System.out.println("sdfhskdjf");
    if(!loggedIn){
    System.out.println("logged = false");
    System.out.println(mainUsername);
    NewsqlLogin.NewsqlLogin();
    execLoop = false;
    }else if(loggedIn){
    System.out.println("logged = else");
    System.out.println("debug " + mainUsername);
    }
    }
    }
    }

    However now the gui does not show!
    Have i deleted too much code somewhere!?

    Thanks for youre help.

    Joe.

  4. #4
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Simple SQL Login to run the main class

    Okay - I've tried a couple different things with the code you gave me, i was unsure where to put it however i guess in the NewsqlLogin.NewsqlLogin() class.
    No, it goes in the Main classes main() method in place of:
    Code:
    NewsqlLogin newsql = new NewsqlLogin();
    The only thing you needed to delete at this stage was setVisible() from NewsqlLogin constructor
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  5. #5
    Join Date
    Feb 2012
    Posts
    6

    Re: Simple SQL Login to run the main class

    Thats superb! All working at the minute,

    Thank you very much - I may be back on later

    Joe.

  6. #6
    Join Date
    Feb 2012
    Posts
    6

    Re: Simple SQL Login to run the main class

    Hey -
    Problem two,
    It still doesn't seem to be re-running the main class when the loggin is completed.
    I cant seem to figure out why the main class does not re execute its code - Although it needs to re-run the whole main logic, does java not do this each time main is called?

    I am completely stuck as to why it doesnt run the main class again -
    when execLoop = true
    loggedIn = true
    menuChoice = 0
    It should execute
    Code:
    final Menu mainMenu = new Menu();
                        EventQueue.invokeAndWait(new Runnable() {
    
                            @Override
                            public void run() {
                                mainMenu.setVisible(true);
                            }
                        });
    However it doesnt!



    Main.java
    Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package computinggraphtransformations;
    
    import java.awt.EventQueue;
    import java.lang.reflect.InvocationTargetException;
    
    /**
     *
     * @author Joey
     */
    public class Main {
    
        public static boolean loggedIn = false;
        public static String mainUsername;
        public static boolean execLoop = true;
        public static int menuChoice = 0;
    //if (Logged == true){
    //}
    
        /**
         * @param args the command line arguments
         */
        public static void main(String args[]) throws InterruptedException, InvocationTargetException {
            //String mainUsername=null;
            //String mainUsername = args[0];
            //System.out.println(args[0]);
            while (execLoop == true) {
    
    
                System.out.println("sdfhskdjf");
                if (loggedIn == false) {
    
    
                    final NewsqlLogin loginPanel = new NewsqlLogin();
                    EventQueue.invokeAndWait(new Runnable() {
    
                        @Override
                        public void run() {
                            loginPanel.setVisible(true);
                            execLoop = false;
                        }
                    });
    
                    loggedIn = loginPanel.isLoggedIn();
    
                    System.out.println("logged = false");
                    System.out.println(mainUsername);
                    
                } else if (loggedIn == true) {
                    System.out.println("logged = else");
                    System.out.println("debug " + mainUsername);
                    System.out.println("");
    
                    if (menuChoice == 0) {
                        final Menu mainMenu = new Menu();
                        EventQueue.invokeAndWait(new Runnable() {
    
                            @Override
                            public void run() {
                                mainMenu.setVisible(true);
                            }
                        });
                    }
    
    
                }
            }
        }
    
        public static void newMain() {
            Main Main = new Main();
    
        }
    }
    newsqlLogin.java
    Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package computinggraphtransformations;
    
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.UnsupportedEncodingException;
    import javax.swing.*;
    import java.security.MessageDigest;
    import java.security.NoSuchAlgorithmException;
    import java.sql.*;
    
    /**
     *
     * @author Joey
     */
    public final class NewsqlLogin extends JFrame {
    
        private boolean success = false;
        public static String username;
    
        public NewsqlLogin() {
            Components();
            this.pack();
            this.setLocationRelativeTo(null);
            //this.setVisible(true);
        }
    
        private void Components() {
            Container c = getContentPane();
    
            //super "Login Window";
    
            //Create the Java components
            JLabel lblUsername = new JLabel("Username: ");
            lblUsername.setHorizontalAlignment(JLabel.CENTER);
            final JTextField txtUsername = new JTextField(10);
            JLabel lblPassword = new JLabel("Password: ");
            lblPassword.setHorizontalAlignment(JLabel.CENTER);
            final JPasswordField txtPassword = new JPasswordField(10);
            txtPassword.setEchoChar('*');
            JButton btnSubmit = new JButton("Submit");
            JButton btnCancel = new JButton("Cancel");
            //btnSubmit.setSize(300, 300);
    
    
            btnSubmit.addActionListener(new ActionListener() {
    
                public void actionPerformed(ActionEvent e) {
                    username = txtUsername.getText().trim();
                    String password = null;
    
                    password = new String(txtPassword.getPassword()).trim();
    
    
    
                    success = submit(username, password);
                    if (success) {
                        JOptionPane.showMessageDialog(null, "Successful login");
                        System.out.println("logged set to true");
                        String id[] = new String[1];
                        id[0] = username;
                        Main.mainUsername = username;
                        Main.loggedIn = true;
                        //Main.main(id);
                        Main.execLoop = true;
                        System.out.println("Logged, Username, Exec Loop");
                        System.out.println(Main.loggedIn);
                        System.out.println(Main.mainUsername);
                        System.out.println(Main.execLoop);
                        System.out.println("New main");
    
                        //new Main();
                        System.out.println("New values");
                        System.out.println(Main.loggedIn);
                        System.out.println(Main.mainUsername);
                        System.out.println(Main.execLoop);
    
    
                        System.out.println("Main class started");
                        System.out.println("menuChoice" + Main.menuChoice);
                        Main.execLoop = true;
                        Main.newMain();
                        close();
                    } else {
                        JOptionPane.showMessageDialog(null, "Incorrect username or password");
                    }
                }
            });
    
            btnCancel.addActionListener(new ActionListener() {
    
                public void actionPerformed(ActionEvent e) {
                    close();
                }
            });
    
    
            //Add the components to the JFrame
            JPanel cp = new JPanel();
            cp.setPreferredSize(new Dimension(300, 100));
            cp.setOpaque(false);
            cp.setLayout(new GridLayout(3, 3, 5, 5));
            cp.add(lblUsername);
            //lblUsername.setSize(300, 300);
            cp.add(txtUsername);
            cp.add(lblPassword);
            cp.add(txtPassword);
            cp.add(btnSubmit);
            cp.add(btnCancel);
    
            c.add(cp);
    
            pack();
            setSize(250, 150);
            //show();
    
    
    
    
        }
    
        protected boolean submit(String username, String password) {
            if (username.length() < 1 || password.length() < 1) {
                return false;
            }
    
            return hashAndVerify(username, password);
        }
    
        private boolean hashAndVerify(String username, String password) {
    
            String query = "SELECT Password FROM userDB WHERE Username = ?";
            PreparedStatement stmt = DatabaseConnection.getStatement(query);
            ResultSet rs = null;
            try {
                stmt.setString(1, username);
                rs = stmt.executeQuery();
                while (rs.next()) {
                    String arr = rs.getString(password);
                    if (password.equals(arr)) {
                        return true;
                    }
                }
            } catch (SQLException e1) {
                e1.printStackTrace();
            } finally {
                try {
                    if (rs != null) {
                        rs.close();
                    }
                    if (stmt != null) {
                        stmt.close();
                    }
                    DatabaseConnection.closeConnection();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
    
            return false;
        }
    
        /*  private byte[] hash(String password) {
        MessageDigest sha1 = null;
        try {
        sha1 = MessageDigest.getInstance("SHA-1");
        
        } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
        }
        
        if (sha1 == null)
        return null;
        
        sha1.update(password);
        return sha1.digest();
        }*/
        protected void close() {
            this.dispose();
        }
    
        boolean isLoggedIn() {
            if (username != null) {
                return true;
            } else {
                return false;
            }
        }
    
        public static void NewsqlLogin() {
            //new NewsqlLogin();
        }
    }
    Menu.java
    Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package computinggraphtransformations;
    
    
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.UnsupportedEncodingException;
    import javax.swing.*;
    import java.security.MessageDigest;
    import java.security.NoSuchAlgorithmException;
    import java.sql.*;
    
    
    /**
     *
     * @author Joey
     */
    
    
    
    
    public final class Menu extends JFrame{
        private boolean success = false;
        public static String username;
    
    //public final username(String username){
    //        this.username.equals(Main.mainUsername);
    //        return Main.username;
    //}
        
         public Menu() {
            Components();
            this.pack();
            this.setLocationRelativeTo(null);
            this.setVisible(true);
        }
        
        private void Components() {
            Container c = getContentPane();
            
            //super "Login Window";
            
            //Create the Java components
            JButton btnLessons = new JButton("Lessons");
            JButton btnTests = new JButton("Tests");
            JButton btnUsercp = new JButton("Upsercp");
            JButton btnLogout = new JButton("Logout");
            JButton btnExit = new JButton("Exit");
            //btnSubmit.setSize(300, 300);
            
            
            
            
            btnLogout.addActionListener( new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    Main.loggedIn = false;
                    Main.mainUsername = null;
                    
                    //JOptionPane.showMessageDialog(, "Successfully Logged Out.");
                    JOptionPane.showConfirmDialog(null, "You have been logged out.");
                    
                    System.out.println(Main.mainUsername);
                    
                }
    
          
                    
                
            });
    
            btnExit.addActionListener( new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    close();
                }
            });
            
            
            //Add the components to the JFrame
            JPanel cp = new JPanel();
            cp.setPreferredSize(new Dimension(300,100));
            cp.setOpaque(false);
            cp.setLayout(new GridLayout(3, 3, 5, 5));
            cp.add(btnLessons);
            cp.add(btnTests);        
            cp.add(btnUsercp);
            cp.add(btnLogout);        
            cp.add(btnExit);
            
            c.add(cp) ;
    
          pack();
          setSize( 250, 150 );
          //show();
          
            
            
            
        }
        
        
         
        protected void close() {
            this.dispose();
        }
        
        
        public static void NewsqlLogin() {    
            //new NewsqlLogin();
            
        }
        
    }
    Thanks again

    Joe.

  7. #7
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Simple SQL Login to run the main class

    I am completely stuck as to why it doesnt run the main class again -
    when execLoop = true
    When you get situations like this get a pen and paper and write down the names and value of each of the variables in the main method. Now, execute the each line in your head and if any of the variables change value amend it on you piece of paper. ie

    Code:
    while (execLoop == true) {
    So is execLoop true? - look on your peice of paper and if it says it is "true" move it the loop else jump to the first line after the end of the loop.
    Code:
    final NewsqlLogin loginPanel = new NewsqlLogin();
    Write down your new variable name and it's value is "the login GUI".
    Code:
                   EventQueue.invokeAndWait(new Runnable() {
    
                        @Override
                        public void run() {
                            loginPanel.setVisible(true);
                            execLoop = false;
                        }
                    });
    Treat this as one - so you pause the main thread and wait for the Event Dispatch Thread to execute the run() method.
    Now step through each line as if you were the event dispatch thread:
    Code:
    loginPanel.setVisible(true);
    Display the GUI
    Code:
     execLoop = false;
    Change the execLoop value on your piece of paper to "false".

    Now carry on doing this until you see where you have gone wrong.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  8. #8
    Join Date
    Feb 2012
    Posts
    6

    Question Re: Simple SQL Login to run the main class

    Hey - I've gone through that, and also try to keep on top of it - hence all the sout's showing different things at different points, however i have seen
    that the whole of
    Code:
    if (loggedIn == false) {
    
    
    
                    final NewsqlLogin loginPanel = new NewsqlLogin();
                    EventQueue.invokeAndWait(new Runnable() {
    
                        @Override
                        public void run() {
                            execLoop = false;
                            System.out.println("Before show pannel");
                            loginPanel.setVisible(true);
                            System.out.println("after show pannel");
                        }
                    });
                
    
                    loggedIn = loginPanel.isLoggedIn();
                    //execLoop = true;
                    System.out.println("logged = false");
                    System.out.println(mainUsername);
                    System.out.println("debuglast?" + loggedIn);
                    
    
                }
    However i presumed with the invoke and wait it should only execute the public void run() bit of it - before it carrys on with the rest of the main method...
    However im guessing after the invoke and wait - it executes the rest of the main method - However since no other conditions are met its not actually doing anything - the loginPannel finishes its run and then the program ends - Build Successful.

    I've tried ending the NewsqlLogin success method with Main.newMain(); however this still doesnt seem to run the main class all over again -

    Either
    - i'm not understanding what the invoke and wait method does (i've tried google'ing it however cant seem to get a answer i can understand)
    - My logic is completely wrong and im not understanding java
    - Or the main method isnt running because i never tell it to run again - again lack of understanding of java

    help! hehe

    thanks

    Joe.

  9. #9
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Simple SQL Login to run the main class

    The main method doesn't run again unless you either explicitly call it again (which you shouldn't do unless you understand the consequences of recursive calls) or you restart the program.

    But you have a while loop in your main method which, whilst it's test is true, stops the main thread exiting the main method (and thus stops the program from terminating). So what makes your loop keep executing?

    Your while loop checks execLoop and if it is true the loop continues but you are setting execLoop to false inside the run() method. So as soon as the invokeAndWait completes and thread of execution gets back to the while loop, the test fails and so the thread exits the main method and the program terminates.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  10. #10
    Join Date
    Feb 2012
    Posts
    6

    Re: Simple SQL Login to run the main class

    Ahh okay - So the while loop will only execute once and as soon as it is false it will never run again?
    If this is the case i see fundamentally where i went wrong
    Doh!!

    However - When the Eventqueue has run the the login dialog it imediatly runs the remaining Main.java code -
    Should it not stop at this point and wait for NewsqlLogin, or loginPanel to stop running or return some value to the eventqueue? this was my interpretation however it seems it doesnt stop the code at all - hence i now have the login and main menue opening at the same time :/


    aka:
    Code:
        public static void main(String args[]) throws InterruptedException, InvocationTargetException {
            //String mainUsername=null;
            //String mainUsername = args[0];
            //System.out.println(args[0]);
    
    
                System.out.println("sdfhskdjf");
                if (loggedIn == false) {
    
    
    
                    final NewsqlLogin loginPanel = new NewsqlLogin();
                    EventQueue.invokeAndWait(new Runnable() {
    
                        @Override
                        public void run() {
                            System.out.println("Before show pannel");
                            loginPanel.setVisible(true);
                            System.out.println("after show pannel");
                        }
                    });
                
    
                    //loggedIn = loginPanel.isLoggedIn();
                    //execLoop = true;
                    System.out.println("logged = false");
                    System.out.println(mainUsername);
                    System.out.println("debuglast?" + loggedIn);
                    
    
                }
                    System.out.println("logged = else");
                    System.out.println("debug " + mainUsername);
                    System.out.println("");
                    if (loggedIn){
                    if (menuChoice == 0) {
                        menuChoice = 100;
                        final Menu mainMenu = new Menu();
                        EventQueue.invokeAndWait(new Runnable() {
    
                            @Override
                            public void run() {
                                mainMenu.setVisible(true);
                            }
                        });
                    }
    
    
                }}    public static void main(String args[]) throws InterruptedException, InvocationTargetException {
            //String mainUsername=null;
            //String mainUsername = args[0];
            //System.out.println(args[0]);
    
    
                System.out.println("sdfhskdjf");
                if (loggedIn == false) {
    
    
    
                    final NewsqlLogin loginPanel = new NewsqlLogin();
                    EventQueue.invokeAndWait(new Runnable() {
    
                        @Override
                        public void run() {
                            System.out.println("Before show pannel");
                            loginPanel.setVisible(true);
                            System.out.println("after show pannel");
                        }
                    });
                
    
                    //loggedIn = loginPanel.isLoggedIn();
                    //execLoop = true;
                    System.out.println("logged = false");
                    System.out.println(mainUsername);
                    System.out.println("debuglast?" + loggedIn);
                    
    
                }
                    System.out.println("logged = else");
                    System.out.println("debug " + mainUsername);
                    System.out.println("");
                    if (loggedIn){
                    if (menuChoice == 0) {
                        menuChoice = 100;
                        final Menu mainMenu = new Menu();
                        EventQueue.invokeAndWait(new Runnable() {
    
                            @Override
                            public void run() {
                                mainMenu.setVisible(true);
                            }
                        });
                    }
    
    
                }}
    Runs the Login window - works fine, sucessfull login, however after the dialog closes the program 'Build successful'
    If i take out the
    Code:
    if(loggedIn){}
    Then it open both the login, then the main menu -
    I need that if statement in, however it doesnt execute the code from high enough up after the login window - which is what i thought the eventqueue was for? unless i have to call a pause and then run method on it somehow?

    thanks

    Joe.
    Last edited by wiinter; February 10th, 2012 at 07:52 AM.

  11. #11
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Simple SQL Login to run the main class

    The problem is you are using JFrame which isn't modal and so you are right the main thread doesn't wait for the panel to close. The real problem though is trying to do this all in the main method isn't really the right way to do it. You have a few possible solutions, such as.

    • The simplest solution albeit it is not to be recommended for any serious applications is to have the main thread display the login panel and then poll the panel every n milliseconds to wait for it to complete.
    • The simplest solution reasonable solution is to change NewsqlLogin to a JDialog and display it as modal with a null parent. This does have a few disadvantages though such as no application button appearing on the taskbar. You can always set the JDialog to be allways on top which should stop it getting hidden behind other panels.
    • Create a main application gui which your main method creates and sets visible and then display the login panel as a modal JDialog with the main gui as the owner.
    • Suspend the main thread using wait() after displaying the login panel and reactivate it using notify() when the login panel closes.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured