Java Reference
In-Depth Information
okButton
.
addActionListener
(
al
);
usernameField
.
addActionListener
(
al
);
passwordField
.
addActionListener
(
al
);
cancelButton
.
addActionListener
(
new
CancelResponse
());
}
private
void
show
()
{
String
prompt
=
this
.
getRequestingPrompt
();
if
(
prompt
==
null
)
{
String
site
=
this
.
getRequestingSite
().
getHostName
();
String
protocol
=
this
.
getRequestingProtocol
();
int
port
=
this
.
getRequestingPort
();
if
(
site
!=
null
&
protocol
!=
null
)
{
prompt
=
protocol
+
"://"
+
site
;
if
(
port
>
0
)
prompt
+=
":"
+
port
;
}
else
{
prompt
=
""
;
}
}
mainLabel
.
setText
(
"Please enter username and password for "
+
prompt
+
": "
);
passwordDialog
.
pack
();
passwordDialog
.
setVisible
(
true
);
}
PasswordAuthentication
response
=
null
;
class
OKResponse
implements
ActionListener
{
@Override
public
void
actionPerformed
(
ActionEvent
e
)
{
passwordDialog
.
setVisible
(
false
);
// The password is returned as an array of
// chars for security reasons.
char
[]
password
=
passwordField
.
getPassword
();
String
username
=
usernameField
.
getText
();
// Erase the password in case this is used again.
passwordField
.
setText
(
""
);
response
=
new
PasswordAuthentication
(
username
,
password
);
}
}
class
CancelResponse
implements
ActionListener
{
@Override
public
void
actionPerformed
(
ActionEvent
e
)
{
passwordDialog
.
setVisible
(
false
);
// Erase the password in case this is used again.
passwordField
.
setText
(
""
);
response
=
null
;
}
}