-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPasswordChooser.java
96 lines (79 loc) · 2.58 KB
/
PasswordChooser.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package dataExchange;
import javax.swing.*;
import java.awt.*;
/**
* A password chooser that is shown inside a dialog
*/
public class PasswordChooser extends JPanel {
private JTextField username;
private JPasswordField password;
private JButton okBtn;
private boolean ok;
private JDialog dialog;
public PasswordChooser() {
setLayout(new BorderLayout());
// construct a panel with user name and password fields
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(2, 2));
panel.add(new JLabel("User name:"));
panel.add(username = new JTextField(""));
panel.add(new JLabel("Password"));
panel.add(password = new JPasswordField(""));
add(panel, BorderLayout.CENTER);
// create Ok and Cancel buttons that terminate the dialog
okBtn = new JButton("OK");
okBtn.addActionListener(e -> {
ok = true;
dialog.setVisible(false);
});
JButton cancelBtn = new JButton("Cancel");
cancelBtn.addActionListener(e -> dialog.setVisible(false));
// add buttons to southern border
JPanel buttonPanel = new JPanel();
buttonPanel.add(okBtn);
buttonPanel.add(cancelBtn);
add(buttonPanel, BorderLayout.SOUTH);
}
/**
* Gets the dialog entries.
*
* @return a user object whose state represents the dialog entries
*/
public User getUser() {
return new User(username.getText(), password.getPassword());
}
/**
* Sets the dialog default.
*
* @param user the default user information
*/
public void setUser(User user) {
username.setText(user.getName());
}
/**
* Show the chooser panel in a dialog
*
* @param parent a component in the owner frame or null
* @param title the dialog window title
*/
public boolean showDialog(Component parent, String title) {
ok = false;
// locate the owner frame
Frame owner = null;
if (parent instanceof Frame)
owner = (Frame) parent;
else
SwingUtilities.getAncestorOfClass(Frame.class, owner);
// if first time, or if owner has changed, make new dialog
if (dialog == null || dialog.getOwner() != owner) {
dialog = new JDialog(owner, true);
dialog.add(this);
dialog.getRootPane().setDefaultButton(okBtn);
dialog.pack();
}
// set title and show dialog
dialog.setTitle(title);
dialog.setVisible(true);
return ok;
}
}