-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPasswordDialog.java
More file actions
130 lines (108 loc) · 3.26 KB
/
PasswordDialog.java
File metadata and controls
130 lines (108 loc) · 3.26 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/* * * * * * * * * * *\
* DefaultGUI.java
* Description: Any feature on the application that we feel needs to be "protected" can use this class which requires the user
* to enter the password of the currently signed in account in order to proceed. If the password is incorrect, an
* error message is shown and prompts the user to retry
* Date: 4/4/16
* @author Brandon Ballard
\* * * * * * * * * * */
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
class PasswordDialog extends JDialog implements ActionListener,DocumentListener
{
JPanel buttonPanel, fieldPanel, blankPanel;
JButton okButton, exitButton;
JPasswordField passwordTF;
JLabel passwordLabel;
String password,empPassword;
public boolean isValid;
public PasswordDialog(String password)
{
this.empPassword = password;
okButton = new JButton("OK");
okButton.setBackground(Color.WHITE);
okButton.addActionListener(this);
getRootPane().setDefaultButton(okButton);
okButton.setEnabled(false);
exitButton = new JButton("Cancel");
exitButton.setBackground(Color.WHITE);
exitButton.addActionListener(this);
buttonPanel = new JPanel(new FlowLayout());
buttonPanel.setBackground(Color.GRAY);
buttonPanel.add(exitButton);
buttonPanel.add(okButton);
blankPanel = new JPanel();
blankPanel.setBackground(new Color(1, 0.1f, 0.1f).darker().darker());
passwordLabel = new JLabel("Password:");
passwordLabel.setForeground(Color.WHITE);
passwordTF = new JPasswordField(20);
passwordTF.getDocument().addDocumentListener(this);
fieldPanel = new JPanel(new FlowLayout());
fieldPanel.setBackground(new Color(1, 0.1f, 0.1f).darker().darker());
fieldPanel.add(passwordLabel);
fieldPanel.add(passwordTF);
//Add components
getContentPane().add(blankPanel, BorderLayout.NORTH);
getContentPane().add(fieldPanel, BorderLayout.CENTER);
getContentPane().add(buttonPanel, BorderLayout.SOUTH);
setupMainFrame();
}
public void actionPerformed(ActionEvent e)
{
if( e.getSource() == okButton )
{
validatePassword();
}
else if(e.getSource() == exitButton )
{
this.dispose();
}
}
//This method will validate the password
void validatePassword()
{
if(new String(passwordTF.getPassword()).equals(empPassword))
{
this.dispose();
isValid = true;
}
else
{
JOptionPane.showMessageDialog(this," Password is incorrect", "Validation Failure" , JOptionPane.ERROR_MESSAGE);
isValid = false;
}
}
public void insertUpdate(DocumentEvent de)
{
password = new String(passwordTF.getPassword());
if(!password.equals(""))
{
okButton.setEnabled(true);
}
}
public void removeUpdate(DocumentEvent de)
{
password = new String(passwordTF.getPassword());
if(password.equals(""))
{
okButton.setEnabled(false);
}
}
public void changedUpdate(DocumentEvent de){}
void setupMainFrame()
{
setTitle("Password Required");
Toolkit tk;
Dimension d;
tk = Toolkit.getDefaultToolkit();
d = tk.getScreenSize();
setSize(d.width/3, d.height/6);
setLocation(d.width/3, d.height/3);
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
setModal(true);
setVisible(true);
}
}