Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Getting Password from Password Field

  1. #1
    Junior Member
    Join Date
    Aug 2014
    Location
    New Zealand
    Posts
    8
    My Mood
    Amazed
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Exclamation Getting Password from Password Field

    Hi, Bradley here.

    I am new to JAVA GUI programming and was wondering how you go about getting and testing if a password field text equals something. For example, if the password equals "password" then do something.

    Any help would be greatly appreciated.

    Cheers, Bradley.


  2. #2
    Member Ada Lovelace's Avatar
    Join Date
    May 2014
    Location
    South England UK
    Posts
    414
    My Mood
    Angelic
    Thanks
    27
    Thanked 61 Times in 55 Posts

    Default Re: Getting Password from Password Field

    With GUI you can use equalsIgnoreCase() or you can use the standard equals() method.
    Remember the equality operator '==' does not work on String in Java.

    Wishes Ada xx
    If to Err is human - then programmers are most human of us all.
    "The Analytical Engine offers a new, a vast, and a powerful language . . .
    for the purposes of mankind
    ."
    Augusta Ada Byron, Lady Lovelace (1851)

  3. #3
    Junior Member
    Join Date
    Aug 2014
    Location
    New Zealand
    Posts
    8
    My Mood
    Amazed
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Angry Re: Getting Password from Password Field

    Hi.

    I have tried the equals() method, but I get a warning saying: ".equals() on incompatible types, flips operands of the binary operator invert if."

    Here is my code:
        private void loginButtonActionPerformed(java.awt.event.ActionEvent evt) {                                            
            if(usernameText.getText().equals("username") && passwordText.equals("password"))
            {
                loginSuccessfulLabel.setText("Login Successful!");
            }
            else
            {
                loginSuccessfulLabel.setText("Login Failed!");
            }
        }

    Is there something I am doing wrong?

    Cheers, Bradley.
    Last edited by tazeunite00; August 26th, 2014 at 03:21 AM. Reason: mistake in first post

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Getting Password from Password Field

    Post runnable code that demonstrates the error.

  5. #5
    Junior Member
    Join Date
    Aug 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Getting Password from Password Field

    you forgot the .getText() in the password Field
        private void loginButtonActionPerformed(java.awt.event.ActionEvent evt) {                                            
            if(usernameText.getText().equals("username") && passwordText.getText().equals("password"))
            {
                loginSuccessfulLabel.setText("Login Successful!");
            }
            else
            {
                loginSuccessfulLabel.setText("Login Failed!");
            }
        }
    now it should work perfectly .

Similar Threads

  1. [SOLVED] Password Field Help.
    By JosPhantasmE in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 22nd, 2013, 07:57 PM
  2. Using a password field
    By javapenguin in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 10th, 2010, 11:54 AM
  3. password field, with focused "Enter" button
    By chronoz13 in forum Java Swing Tutorials
    Replies: 1
    Last Post: June 13th, 2010, 05:00 PM
  4. password field, with focused "Enter" button
    By chronoz13 in forum Java Code Snippets and Tutorials
    Replies: 1
    Last Post: June 13th, 2010, 05:00 PM
  5. password
    By 5723 in forum Algorithms & Recursion
    Replies: 9
    Last Post: July 9th, 2009, 05:26 AM

Tags for this Thread