下面列出了javax.swing.JPasswordField#getPassword ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private PasswordAuthentication getCredential(String msg, boolean proxy) {
JTextField user = new JTextField(30);
JPasswordField pass = new JPasswordField(30);
String prompt = proxy ? StringResource.get("PROMPT_PROXY")
: String.format(StringResource.get("PROMPT_SERVER"), msg);
Object[] obj = new Object[5];
obj[0] = prompt;
obj[1] = StringResource.get("DESC_USER");
obj[2] = user;
obj[3] = StringResource.get("DESC_PASS");
obj[4] = pass;
if (JOptionPane.showOptionDialog(null, obj, StringResource.get("PROMPT_CRED"), JOptionPane.OK_CANCEL_OPTION,
JOptionPane.PLAIN_MESSAGE, null, null, null) == JOptionPane.OK_OPTION) {
PasswordAuthentication pauth = new PasswordAuthentication(user.getText(), pass.getPassword());
return pauth;
}
return null;
}
public static String showUnlockWalletDialog()
{
JPanel userPanel = new JPanel();
userPanel.setLayout(new GridLayout(2,2));
//Labels for the textfield components
JLabel passwordLbl = new JLabel("Enter wallet password:");
JPasswordField passwordFld = new JPasswordField();
//Add the components to the JPanel
userPanel.add(passwordLbl);
userPanel.add(passwordFld);
//As the JOptionPane accepts an object as the message
//it allows us to use any component we like - in this case
//a JPanel containing the dialog components we want
if(JOptionPane.showConfirmDialog(null, userPanel, "Unlock Wallet" ,JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE) == JOptionPane.OK_OPTION)
{
return new String(passwordFld.getPassword());
}
return "";
}
public RemoteTopology buildRemoteTopology() {
String localSharedFolderPath = getLocalSharedFolderPath();
String localPassword = null;
JPasswordField localPasswordTextField = getLocalPasswordTextField();
if (localPasswordTextField != null) {
localPassword = new String(localPasswordTextField.getPassword());
}
RemoteTopology remoteTopology = new RemoteTopology(getRemoteSharedFolderPath(), getRemoteUsername(), getRemotePassword());
remoteTopology.setLocalMachineData(localSharedFolderPath, localPassword);
ListModel<RemoteMachineProperties> listModel = getRemoteMachinesList().getModel();
for (int i=0; i<listModel.getSize(); i++) {
remoteTopology.addRemoteMachine(listModel.getElementAt(i));
}
return remoteTopology;
}
@Override
public String readPassword(String promptMessage) {
final JPasswordField jpf = new JPasswordField();
JOptionPane jop = new JOptionPane(jpf,
JOptionPane.QUESTION_MESSAGE,
JOptionPane.OK_CANCEL_OPTION);
JDialog dialog = jop.createDialog(promptMessage);
dialog.addComponentListener(new ComponentAdapter() {
@Override
public void componentShown(ComponentEvent e) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
jpf.requestFocusInWindow();
}
});
}
});
dialog.setVisible(true);
int result = (Integer) jop.getValue();
dialog.dispose();
String password = null;
if (result == JOptionPane.OK_OPTION) {
password = new String(jpf.getPassword());
}
if (StringUtils.isEmpty(password)) {
return null;
} else {
return password;
}
}
@Override
public String inputPassword(String messageText) {
final JPasswordField passwordField = new JPasswordField();
JOptionPane jop = new JOptionPane(new Object[] { messageText, passwordField }, JOptionPane.QUESTION_MESSAGE,
JOptionPane.OK_CANCEL_OPTION);
JDialog dialog = jop.createDialog("Authentication required");
dialog.addComponentListener(new ComponentAdapter() {
@Override
public void componentShown(ComponentEvent e) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
passwordField.requestFocusInWindow();
passwordField.requestFocus();
}
});
}
});
dialog.setVisible(true);
int result = (Integer) jop.getValue();
if (result == JOptionPane.OK_OPTION) {
return new String(passwordField.getPassword());
} else {
return null;
}
}
@Override
public void actionPerformed(final ActionEvent e) {
final JLabel label = new JLabel("Enter Password, (Leave blank for no password).");
final JPasswordField passwordField = new JPasswordField();
final JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.add(label, BorderLayout.NORTH);
panel.add(passwordField, BorderLayout.CENTER);
final int selectedOption =
JOptionPane.showOptionDialog(
JOptionPane.getFrameForComponent(parent),
panel,
"Enter Password",
JOptionPane.OK_CANCEL_OPTION,
JOptionPane.PLAIN_MESSAGE,
null,
null,
null);
if (selectedOption != JOptionPane.OK_OPTION) {
return;
}
final String password = new String(passwordField.getPassword());
final boolean passworded;
if (!password.isBlank()) {
validator.setGamePassword(password);
passworded = true;
} else {
validator.setGamePassword(null);
passworded = false;
}
if (lobbyWatcher != null && lobbyWatcher.isActive()) {
lobbyWatcher.setPassworded(passworded);
}
}
@Override
public void collect() throws CredentialCollectionException {
JLabel jl = new JLabel("Enter Your username: ");
JTextField juf = new JTextField(24);
JLabel jl2 = new JLabel("Enter Your password: ");
JPasswordField jpf = new JPasswordField(24);
Box box1 = Box.createHorizontalBox();
box1.add(jl);
box1.add(juf);
Box box2 = Box.createHorizontalBox();
box2.add(jl2);
box2.add(jpf);
Box box = Box.createVerticalBox();
box.add(box1);
box.add(box2);
// JDK-5018574 : Unable to set focus to another component in JOptionPane
SwingUtils.workAroundFocusIssue(juf);
int x = JOptionPane.showConfirmDialog(null, box,
"KnoxShell Login", JOptionPane.OK_CANCEL_OPTION);
if (x == JOptionPane.OK_OPTION) {
ok = true;
username = juf.getText();
pass = jpf.getPassword();
}
}
/**
* Ask using a GUI for the username and password.
*/
private void readFromGUI() {
// Create fields for user name.
final JTextField usernameField = new JTextField(20);
usernameField.setText(this.username);
final JLabel usernameLabel = new JLabel("Username: ");
usernameLabel.setLabelFor(usernameField);
final JPanel usernamePane = new JPanel(new FlowLayout(FlowLayout.TRAILING));
usernamePane.add(usernameLabel);
usernamePane.add(usernameField);
// Create fields for password.
final JPasswordField passwordField = new JPasswordField(20);
passwordField.setText(this.password);
final JLabel passwordLabel = new JLabel("Password: ");
passwordLabel.setLabelFor(passwordField);
final JPanel passwordPane = new JPanel(new FlowLayout(FlowLayout.TRAILING));
passwordPane.add(passwordLabel);
passwordPane.add(passwordField);
// Create panel
final JPanel main = new JPanel();
main.setLayout(new BoxLayout(main, BoxLayout.PAGE_AXIS));
main.add(usernamePane);
main.add(passwordPane);
// Create and handle dialog
final JOptionPane jop = new JOptionPane(main, JOptionPane.QUESTION_MESSAGE, JOptionPane.OK_CANCEL_OPTION);
final JDialog dialog = jop.createDialog("User name and password");
dialog.addComponentListener(new ComponentAdapter() {
public void componentShown(ComponentEvent e) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
if (usernameField.getText().isEmpty())
{
usernameField.requestFocusInWindow();
}
else
{
passwordField.requestFocusInWindow();
}
}
});
}
});
dialog.setVisible(true);
final Integer result = (Integer) jop.getValue();
dialog.dispose();
if (result.intValue() == JOptionPane.OK_OPTION) {
this.username = usernameField.getText();
final char[] pwd = passwordField.getPassword();
this.password = new String(pwd);
}
}