Results 1 to 6 of 6
  1. #1
    Gloomy is offline Member
    Join Date
    Jul 2010
    Location
    Wollongong, NSW, Australia
    Posts
    6
    Rep Power
    0

    Default Login window before application startup

    Hey im very new to gui's and not sure what the most appropriate way I should do something...

    What im trying to get is something like a login window, that asks for username and password then checks that by connecting to a database, before activating the main application. Im not sure if what im looking for is a dialog window or 2 seperate JFrames, any suggestions?

    Ive got something working with 2 different JFrames, one for login and one for the main app, but I didnt know how to implement a check to see if the login JFrame had been run first, is there a way I could do this? I could pass information between the two JFrames but couldnt get that check thing figured out. I just dont want it to be easy for someone to open the main application JFrame without it being called by the Login JFrame.

    If there is a better way of doing this I would really love to know :)

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I think that there are many ways to skin this cat including showing one JFrame after another, or using a modal JDialog, or using CardLayout to swap JPanels,... and so forth. Just try to get something that works, and if it doesn't, we can help you work it out. Now if you're talking about a professional application then security considerations are paramount and precautions will need to be taken.

  3. #3
    Gloomy is offline Member
    Join Date
    Jul 2010
    Location
    Wollongong, NSW, Australia
    Posts
    6
    Rep Power
    0

    Default

    Thanks for the reply :) its just a little school project, Ive got a basic setup now where login window calls the mainapp if the user exists in the database, is there a way that the mainapp will only open if its called from the login? they are both jframes.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Myself, I'd try to keep things as simple as possible by making may sign-in GUI a JPanel that has just a few components:
    a JLabel: "Name"
    a JTextField: nameField
    another JLabel: "Password"
    and a JPasswordField: passwordField.

    I'd give it two public methods, getName and getPassword()
    Java Code:
    public String getName() {
      // return a String, the text in the nameField
    }
    
    public char[] getPassword() {
      // return a char array, the char[] held by the passwordField
    }
    I'd then create an instance of this JPanel, say called signInPanel, and simply display it in a JOptionPane.showConfirmDialog:
    Java Code:
          int response = JOptionPane.showConfirmDialog(null, signInJPanel, "Log In", 
                JOptionPane.OK_CANCEL_OPTION);
          if (response == JOptionPane.OK_OPTION) {
             // extract the user name and password from the signInJPanel, and test if in database. 
             // if so, then start applicaiton
          }

  5. #5
    mrp3rf3ct83 is offline Member
    Join Date
    Jul 2010
    Posts
    6
    Rep Power
    0

    Default

    I recently did a program that implemented a login/password frame, the easiest way was that in order to that was to give each user a login and password or by just having one for just the administrator. Depending on which one it is you choose, if it is just the administrator, you should have a separate file located in the same package as the login/password frame; that way you can just read the file without putting in hard coding in the code so anyone can see it. If it is to every user you want a login/password, you have to create code to make login/password, then give the users their login and default password or just hard code the logins/passwords directly into the database. Passwords can be Strings too, just make them case sensitive (e.g. equals instead of equalsIgnoreCase). Make the login uneditable. Here's the code from reading from a file to get the login/password:

    This is the notepad file for the login/password. The simplest way for me:

    url=jdbc: odbc:Name of the database here
    username="login here"
    password="password here"

    Load the file:

    Properties connectProperties = loadProperties(loginPasswordFile);
    String dbUrl = connectProperties.getProperty("url");
    String username = connectProperties.getProperty("username");
    String password = connectProperties.getProperty("password");
    Connection connection = DriverManager.getConnection(dbUrl, username, password); // This loads the database

    You have to throw an Exception for both these methods shown in case the file/database is not found. The easiest way to do it is to just put throws Exception in your method declaration:

    public void method() throws Exception {}

    If you are hard coding the logins/passwords to each user you have to connect to the database before you start. To get the textField, you just call the object it's associated with:

    login = nameField.getText();
    password = passwordField.getText();

    Connecton connection = DriverManager.getConnection("jdbc: odbc:Name of the database here");
    String queryStatement = "Select * from Database where login= ? and password = ?";
    PreparedStatement ps = connection.prepareStatement(queryStatement);
    ps.setString(1,login);
    ps.setString(2,password);
    ResultSet rs = ps.executeQuery();
    if (rs != null){
    // code that displays the database
    } else {
    JOptionPane.showMessageDialog(null,"Username/Password Incorrect. Try Again!!!","Error",JOptionPane.ERROR_MESSAGE);
    }

    Before you try connecting to the database, you have to find a driver to read it this is the best way; it automatically finds it for you:

    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); // This goes before both examples above
    Last edited by mrp3rf3ct83; 07-19-2010 at 09:13 PM.

  6. #6
    mrp3rf3ct83 is offline Member
    Join Date
    Jul 2010
    Posts
    6
    Rep Power
    0

    Default

    You should have a different class for managing frames and panels. When you implement the actionPerformed(ActionEvent e) method, you should have that take you to the class and the appropriate method to open the right frame. Read up on MVC design. It basically says keep what the user sees, the logic and processing separate by have classes that communicate with both.

Similar Threads

  1. Replies: 0
    Last Post: 12-03-2009, 06:46 AM
  2. webcam application startup
    By Bharat in forum New To Java
    Replies: 7
    Last Post: 01-19-2009, 05:24 AM
  3. How to hide a window of application
    By Jktu in forum AWT / Swing
    Replies: 4
    Last Post: 10-29-2008, 06:53 AM
  4. How to write the login application using cookies
    By lukky in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 08-19-2008, 06:06 PM
  5. Developing Window based application
    By hisouka in forum New To Java
    Replies: 7
    Last Post: 08-11-2008, 05:12 AM

Posting Permissions

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