-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGetPassword.java
More file actions
152 lines (134 loc) · 5.71 KB
/
GetPassword.java
File metadata and controls
152 lines (134 loc) · 5.71 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package javaforce;
/*
* GetPassword.java
*
* Created on August 3, 2007, 1:57 PM
*
* @author pquiring
*/
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
public class GetPassword extends javax.swing.JDialog {
/**
* Creates new form GetPassword
*/
public GetPassword(java.awt.Frame parent, boolean modal) {
super(parent, modal);
initComponents();
setComponentOrientation(((parent == null) ? javax.swing.JOptionPane.getRootFrame() : parent).getComponentOrientation());
if (parent != null) {
setLocationRelativeTo(parent); //doesn't work
}
setPosition();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
label = new javax.swing.JLabel();
password = new javax.swing.JPasswordField();
bOk = new javax.swing.JButton();
bCancel = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
setTitle("Enter Password");
setResizable(false);
label.setText("Password");
password.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(java.awt.event.KeyEvent evt) {
passwordKeyPressed(evt);
}
});
bOk.setText("OK");
bOk.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bOkActionPerformed(evt);
}
});
bCancel.setText("Cancel");
bCancel.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bCancelActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(label)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(password, javax.swing.GroupLayout.DEFAULT_SIZE, 148, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addComponent(bOk)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 82, Short.MAX_VALUE)
.addComponent(bCancel)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(label)
.addComponent(password, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(bOk)
.addComponent(bCancel))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void passwordKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_passwordKeyPressed
if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
bOkActionPerformed(null);
}
if (evt.getKeyCode() == KeyEvent.VK_ESCAPE) {
bCancelActionPerformed(null);
}
}//GEN-LAST:event_passwordKeyPressed
private void bCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bCancelActionPerformed
retValue = null;
setVisible(false);
}//GEN-LAST:event_bCancelActionPerformed
private void bOkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bOkActionPerformed
retValue = new String(password.getPassword());
if (retValue.length() == 0) {
return;
}
setVisible(false);
}//GEN-LAST:event_bOkActionPerformed
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton bCancel;
private javax.swing.JButton bOk;
private javax.swing.JLabel label;
private javax.swing.JPasswordField password;
// End of variables declaration//GEN-END:variables
private String retValue = null;
public static String getPassword(Frame parent) {
return getPassword(parent, null);
}
public static String getPassword(Frame parent, String prompt) {
GetPassword dialog = new GetPassword(parent, true);
if (prompt != null) {
dialog.label.setText(prompt);
dialog.pack();
}
dialog.setVisible(true); //does not return until dialog is closed
return dialog.retValue;
}
private void setPosition() {
Rectangle s = GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds();
Dimension d = getPreferredSize();
setLocation(s.width / 2 - d.width / 2, s.height / 2 - (d.height / 2));
}
}