Hi everyone,

I really need some advice and guidance on the following code

import javax.swing.*;
import java.util.*;

public class PopupAuthenticator 
    extends Authenticator {

  public PasswordAuthentication 
      getPasswordAuthentication() {
    String username, password;

    String result = JOptionPane.showInputDialog(
      "Enter 'username,password'");

    StringTokenizer st = 
      new StringTokenizer(result, ",");
    username = st.nextToken();
    password = st.nextToken();

    return new PasswordAuthentication(
      username, password);
  }
}

As you guys see above there is JOptionPane that requests both the username and password to access a http website, but i have a dilemma about someting about the method getPasswordAuthentication() that i am supposed to overide.

The thing is i am afraid the getPasswordAuthentication() method may actually return the value of the of both the username and password before the user has even had the chance to type in the correct
username and password.

As you guys can see that the gui that i set gets the value of the input optionpane and i was hoping that that the function returns after the user had pressed "OK" but the thing is i suspect that the function may be returning as soon as the option pane is shown thus causing both the values of the username and password to be null.

Am i using the Authenticator class with gui the correct way or am i missing something??

I really hope someone can explain to me in detail on how this Authenticator class works and maybe help suggest a possible solution that can help me out of my dillema.

Any help is greatly appreciated

Thank You

Yours Sincerely

Richard West

Hi Richard,

The JOptionPane.showInputDialog should NOT return until the user closes the Dialog, I quote from the JOptionPane API Class documentation:

All dialogs are modal. Each showXxxDialog method blocks the current thread until the user's interaction is complete.

Have you tried doing a System.out.println of the username & password variables after your call to the JOptionPane? That way you can open the dialog and see if the code continues while the dialog is still open. You should only see the println output once the dialog is closed.

The only other thing I can see which jumped out at me was that you are passing a String Object into the PasswordAuthentication constructor, but the definition of the Constructor is:

PasswordAuthentication(String userName, char[] password)

The password argument is a char array. Can you pass a String Object into a char[]? I don't know, Ive never tired it. In any case you could try changing your return statement as below:

return new PasswordAuthentication(username, password.toCharArray());

Hope it helps,

Kate

Hi everyone,

This is what i did

import javax.swing.*;
import java.util.*;

public class PopupAuthenticator 
    extends Authenticator {

  public PasswordAuthentication 
      getPasswordAuthentication() {
    String username, password;

    String result = JOptionPane.showInputDialog(
      "Enter 'username,password'");

    StringTokenizer st = 
      new StringTokenizer(result, ",");
    username = st.nextToken();
    password = st.nextToken();

System.out.println("You are alomost returning");
    return new PasswordAuthentication(
      username, password.toCharArray());
System.out.println("You have returned");
  }
}

It seem that my worst fears were founded because as soon the JOptionPane showed,
the following two line were printed out

"You are alomost returning"
"You have returned"

From this result i assumed that the function has returned even before the user has a chance to type in his user name and password. I tried using a JDialog and even a JFrame in place of the option pane but i got the same result

Kate Albany you also said this
"All dialogs are modal. Each showXxxDialog method blocks the current thread until the user's interaction is complete"

Can this rule also apply for JDialogs or maybe even JFrames??

As for the char array thing sorry about that as it was a careless mistake

Hoping to hear from you

Thank You

Yours Sincerely

Richard West

Morning Richard,

Now that has me rather confused. You should never have seen the output from:

System.out.println("You have returned");

As this should be an 'Unreachable Statement'. The 'return' just before it should exit the function altogether and never execute that line.

However, back to the JOptionPane problem. Try calling this version of showInputDialog:

public static String showInputDialog(Component parentComponent, Object message) throws HeadlessException

Maybe it needs the 'parentComponent' to stop the execution of that Components thread. Also I guess that a 'HeadlessException' isn't being thrown?

I don't believe JFrame's or JDialog's can block the underlying thread.

To be honest I do not know why 'showInputDialog' is not blocking your thread, I have used it many times and never encountered this problem.

Kate

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.