How to create an interface asking Username and correct password?..

edited September 2015 in How To...

Hello,

Excuse me, I contact you because I start to use processing but I need your help.

In fact I have 2 tab, and I want to create a tab or something like that who is before these two other tabs where we have to enter an username and a password, if password ok, you « switch » on the other tab and you can do what you want, if the password is wrong, you can’t access once the password is not good.. :/

Is it possible to do something like that or not?

I see that someone do that

import java.awt.*;

import java.awt.event.*;

void setup(){ size(600, 600); Label l1 = new Label("Enter Username"),

l2 = new Label("Enter password");

TextField t1 = new TextField("",45), t2 = new TextField(20);

t2.setEchoChar('*');
add(l1);
add(t1);
add(l2);
add(t2);

}

i don’t know if it can help you..

Sincerely, Alexis.

Answers

  • A simple example of using text dialog inputs:

    /** 
     * No Repeat ID Input (v1.10)
     * by GoToLoop (2013/Nov)
     * 
     * forum.processing.org/two/discussion/869
     * /check-array-contents-with-arraylist
     */
    
    import static javax.swing.JOptionPane.*;
    
    final StringList ids = new StringList( new String[] {
      "Eric", "Beth", "Katniss"
    } 
    );
    
    void draw() {
      println(ids);
    
      final String id = showInputDialog("Please enter new ID");
    
      if (id == null)   exit();
    
      else if ("".equals(id))
        showMessageDialog(null, "Empty ID Input!!!", 
        "Alert", ERROR_MESSAGE);
    
      else if (ids.hasValue(id))
        showMessageDialog(null, "ID \"" + id + "\" exists already!!!", 
        "Alert", ERROR_MESSAGE);
    
      else {
        showMessageDialog(null, "ID \"" + id + "\" successfully added!!!", 
        "Info", INFORMATION_MESSAGE);
        ids.append(id);
      }
    }
    
  • edited December 2013

    Hello, thank you for your help but now I have some problem. First processing tell me "No library found for static javax.swing.JOptionPane" So I don't know if I have to download it or something like that :/

    import static javax.swing.JOptionPane.*;

    final StringList ids = new StringList( new String[] { "Baptiste", "Tanguy", "Alexis" } );

    void draw() { println(ids);

    final String id = showInputDialog("Entre password.");

    if (id == null) exit();

    else if ("".equals(id)) showMessageDialog(null, "Incorrect password", "Alert", ERROR_MESSAGE);

    else if (ids.hasValue(id)) showMessageDialog(null, " Done, click on Ok.", "Info", INFORMATION_MESSAGE);

    else { showMessageDialog(null, "Incorrect password.", "Alert", ERROR_MESSAGE); ids.append(id); } }

    And now I want to enter a password, so how can I hide the text with "*" ? :/ Thank you again..

  • edited July 2014

    "No library found for static javax.swing.JOptionPane"

    It's just a glitch! Otherwise, we wouldn't be able to use its methods & fields like showInputDialog() or ERROR_MESSAGE! :-j

    so how can I hide the text with "*" ?

    I've had to search for it and found this 1 out:
    http://www.coderanch.com/t/341807/GUI/java/JOptionPane-password-input

    Their fix is intermingle JOptionPane + JPasswordField!
    And below's my own take on it: <:-P

    /** 
     * Hidden Password Input (v1.1)
     * by GoToLoop (2013/Dec)
     * 
     * forum.processing.org/two/discussion/1724/
     * how-to-create-an-interface-asking-username-and-correct-password-
     *
     * forum.processing.org/two/discussion/6556
     * is-it-possible-to-create-a-user-login-using-processing
     */
    
    import static javax.swing.JOptionPane.*;
    import javax.swing.JPasswordField;
    
    final StringDict accounts = new StringDict(
    new String[] {
      "baptiste", "tanguy", "alexis"
    }
    , new String[] {
      "bap", "guy", "ex"
    }
    );
    
    final JPasswordField pwd = new JPasswordField();
    
    {
      println(accounts);
    }
    
    void draw() {
      if (frameCount == 1)  frame.setVisible(false);
    
      String user = askUser();
    
      if (user == null)           confirmQuit();
      else if (!"".equals(user))  askPass(user);
    }
    
    String askUser() {
      String id = showInputDialog("Please enter user:");
    
      if (id == null)
        showMessageDialog(null, "You've canceled login operation!"
          , "Alert", ERROR_MESSAGE);
    
      else if ("".equals(id))
        showMessageDialog(null, "Empty user input!"
          , "Alert", ERROR_MESSAGE);
    
      else if (!accounts.hasKey(id = id.toLowerCase()))
        showMessageDialog(null, "Unknown \"" + id + "\" user!" + (id = "")
          , "Alert", ERROR_MESSAGE);
    
      return id;
    }
    
    boolean askPass(String id) {
      boolean isLogged = false;
      pwd.setText("");
    
      int action = showConfirmDialog(null, pwd
        , "Now enter password:", OK_CANCEL_OPTION);
    
      if (action != OK_OPTION) {
        showMessageDialog(null, "Password input canceled!"
          , "Alert", ERROR_MESSAGE);
    
        return false;
      }
    
      String phrase = pwd.getText();
      //String phrase = new String(pwd.getPassword());
    
      if ("".equals(phrase))
        showMessageDialog(null, "Empty password input!"
          , "Alert", ERROR_MESSAGE);
    
      else if (accounts.get(id).equals(phrase)) {
        showMessageDialog(null, "Welcome \"" + id + "\"!\nYou're logged in!"
          , "Info", INFORMATION_MESSAGE);
    
        isLogged = true;
      }
    
      else
        showMessageDialog(null, "Password \"" + phrase + "\" mismatch!"
          , "Alert", ERROR_MESSAGE);
    
      return isLogged;
    }
    
    void confirmQuit() {
      if (showConfirmDialog(null, "Wanna quit then?", "Exit"
        , OK_CANCEL_OPTION) == OK_OPTION)  exit();
    }
    
  • Oh okay :p but....thank you very much, it works a charm! ^:)^ Sorry for my english because I'm french, but it's just perfect! Exactly what I want, thank you again! :)

  • Hello,

    We are making a game for school and we've tried to implement this code into our code so that the popup screen would popup when you clicked on a certain position on the background. However, this did not work. it gave us the error that we missed a semi colon wich made us very confused cause if we used this code seperatly it did work perfectly fine. can you help us ?

  • Post your Code

Sign In or Register to comment.