Results 1 to 3 of 3
  1. #1
    jim
    jim is offline Member
    Join Date
    Oct 2011
    Posts
    3
    Rep Power
    0

    Default Java - Swing - LOGIN Page

    Dear All

    Instead of manualy feeding the user name and password in the code mention below , if i want to connect to MYSQL Database and get the username and password and if it match then allow . Can you please provide with the code


    import java.awt.Dimension;
    import java.awt.GridLayout;

    import javax.swing.*;

    public class LoginTest {
    private static void createAndShowUI() {
    LoginPanel login = new LoginPanel();
    int response = JOptionPane.showConfirmDialog(null, login, "Please Enter Name and Password",
    JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);

    if (response == JOptionPane.OK_OPTION) {
    String name = login.getName();
    String pWord = login.getPassword();

    if (name.equals("Fubar") && pWord.equals("Snafu")) {
    JFrame frame = new JFrame("Main Application");
    JLabel label = new JLabel("Main Application", SwingConstants.CENTER);
    label.setPreferredSize(new Dimension(400, 300));
    frame.getContentPane().add(label);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
    }

    else {
    String msg = "Incorrect name and password. \n(try Fubar and Snafu)";
    JOptionPane.showMessageDialog(null, msg);
    }
    }

    }

    public static void main(String[] args) {
    java.awt.EventQueue.invokeLater(new Runnable() {
    public void run() {
    createAndShowUI();
    }
    });
    }
    }

    class LoginPanel extends JPanel {
    private JTextField nameField = new JTextField(10);
    private JPasswordField passwordField = new JPasswordField(10);

    public LoginPanel() {
    setLayout(new GridLayout(2, 2, 5, 5)); // change to GridBagLayout later
    add(new JLabel("Name:"));
    add(nameField);
    add(new JLabel("Password:"));
    add(passwordField);
    }

    public String getName() {
    return nameField.getText();
    }

    public String getPassword() {
    return new String(passwordField.getPassword()); // shouldn't do this!
    }
    }

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

    Default Re: Java - Swing - LOGIN Page

    Quote Originally Posted by jim View Post
    Instead of manualy feeding the user name and password in the code mention below , if i want to connect to MYSQL Database and get the username and password and if it match then allow . Can you please provide with the code
    Would you like to have a seat, kick your shoes off and help yourself to some doughnuts while we do your work for you?

    Sorry but it doesn't work that way as we're volunteers and not here for this purpose. I suggest that you look for the tutorial for what you're trying to do, and then if you get stuck show us your code (with code tags so we can read it), your errors and perhaps we'll be able to help.

  3. #3
    jim
    jim is offline Member
    Join Date
    Oct 2011
    Posts
    3
    Rep Power
    0

    Default Re: Java - Swing - LOGIN Page

    I am really sorry , and i totaly agree with your point , i had written the code but i forgot to add between . Actually i had gone through some tutorial but still could not find a solution .

    Java Code:
    import java.awt.Dimension;
    import java.awt.GridLayout;
     
    import javax.swing.*;
     
    public class LoginTest {
       private static void createAndShowUI() {
          LoginPanel login = new LoginPanel();
          int response = JOptionPane.showConfirmDialog(null, login, "Please Enter Name and Password",
                   JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
           
          if (response == JOptionPane.OK_OPTION) {
             String name = login.getName();
             String pWord = login.getPassword();
              
             if (name.equals("Fubar") && pWord.equals("Snafu")) {
                JFrame frame = new JFrame("Main Application");
                JLabel label = new JLabel("Main Application", SwingConstants.CENTER);
                label.setPreferredSize(new Dimension(400, 300));
                frame.getContentPane().add(label);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
             }
              
             else {
                String msg = "Incorrect name and password. \n(try Fubar and Snafu)";
                JOptionPane.showMessageDialog(null, msg);
             }
          }
           
       }
     
       public static void main(String[] args) {
          java.awt.EventQueue.invokeLater(new Runnable() {
             public void run() {
                createAndShowUI();
             }
          });
       }
    }
     
    class LoginPanel extends JPanel {
       private JTextField nameField = new JTextField(10);
       private JPasswordField passwordField = new JPasswordField(10);
        
       public LoginPanel() {
          setLayout(new GridLayout(2, 2, 5, 5)); // change to GridBagLayout later
          add(new JLabel("Name:"));
          add(nameField);
          add(new JLabel("Password:"));
          add(passwordField);
       }
        
       public String getName() {
          return nameField.getText();
       }
        
       public String getPassword() {
          return new String(passwordField.getPassword()); // shouldn't do this!
       }
    }
    Attached Files Attached Files

Similar Threads

  1. Java Swing Login Form
    By sehudson in forum Advanced Java
    Replies: 5
    Last Post: 06-01-2010, 12:53 PM
  2. Java Login Page
    By turkuaz07 in forum Advanced Java
    Replies: 1
    Last Post: 03-14-2009, 10:18 PM
  3. login page
    By banie in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 09-09-2008, 09:37 AM
  4. JSP login page
    By banie in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 09-06-2008, 05:23 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
  •