Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Authenticator  RSS feed

 
Richard West
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,

I really need some advice and guidance on the following code



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

ps. I have posted this question here and the socket forum as i am not sure to whether this is a gui problem or a socket problem
 
Joe Ess
Bartender
Posts: 9425
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a look at the documentation for javax.swing.JOptionPane.
All dialogs are modal. Each showXxxDialog method blocks the current thread until the user's interaction is complete.

Therefore this code stops executing at the line:

until the user presses "OK". Why don't you try putting some System.out.println() statements in the getPasswordAuthentication() method so you can observe it working. Maybe print out the user name and password so you can see that you are getting the values you think you are getting.
 
Richard West
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,

This is what i did



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

"You are all most 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

In the JOptionPane Api this is what it says

"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?

Is it possible to make a JFrame modal??

Hoping to hear from you

Thank You

Yours Sincerely

Richard West
 
hatim osman
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider this..

GUIAthu extends Authenticator implements ActionListener{

private JDialog dialog; //create a modal dialog
private JPasswordAuthentication passAut = null;
private JTestField userName;
private JPasswordField password;
private JButton ok;
private JButton cancel;

public GUIAthu() {
//establish your desired GUI

ok.addActionListener(this);
cancel.addActionListener(this);
}


public getPasswordAuthentication() {

//this will hang the thread
dialog.setVisible();

return passAut;
}

public void actionPerformed(ActionEvent e) {
if (e.getSource() == ok) {
String user = userName.getText();
char[] pass = password.getpassord();
passAut = new PasswordAuthentication(user, pass);
} else {
passAut = null;
}
}
}

this is just a framework, it gives an idea how it works
 
Joe Ess
Bartender
Posts: 9425
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by West Richard:

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


You are assuming incorrectly. Why didn't you try printing out the values entered from the dialog? This line blocks:

But for some strange reason you put your println's around some lines which are not blocked by the dialog:

Actually I'm surprised that this code compiles. I'd expect that you'd get an "unreachable statement" error on that second System.out.println.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!