forked from kasarrine/voterproject
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextField.java
More file actions
149 lines (131 loc) · 2.48 KB
/
TextField.java
File metadata and controls
149 lines (131 loc) · 2.48 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
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
//import java.awt.event.KeyEvent;
//import java.awt.event.KeyListener;
import javax.swing.JTextField;
@SuppressWarnings("serial")
public class TextField extends JTextField implements FocusListener
{
private String placeholder;
private boolean isDefault;
//private boolean isPassword;
//private String text;
//private int pos;
public TextField()
{
//pos = 0;
//text = "";
isDefault = true;
this.addFocusListener(this);
/*
this.addKeyListener(new KeyListener() {
@Override
public void keyPressed(KeyEvent key)
{
if(key.getKeyCode() == 37)
{
pos = Math.max(pos-1, 0);
}
else if(key.getKeyCode() == 39)
{
pos = Math.min(pos+1, text.length());
}
else if(key.getKeyCode() == 8 && pos >= 1)
{
if(text.length() >= 1)
{
text = text.substring(0,pos-1)+text.substring(pos);
pos--;
}
}
else if((""+key.getKeyChar()).matches("[a-zA-z0-9`~!@#\\$%\\^&*\\(\\)\\-_=+\\[\\{\\]\\}'\"/?,<.> ]"))
{
text = text.substring(0,pos)+key.getKeyChar()+text.substring(pos);
pos++;
}
}
@Override
public void keyReleased(KeyEvent key)
{
if((""+key.getKeyChar()).matches("[a-zA-z0-9`~!@#\\$%\\^&*\\(\\)\\-_=+\\[\\{\\]\\}'\"/?,<.> ]") || key.getKeyCode()==8)
{
st();
setText(text);
pos = text.length();
}
}
@Override
public void keyTyped(KeyEvent key) {}
});*/
}
public boolean getIsDefault()
{
return isDefault;
}
public void setPlaceholder(String s)
{
placeholder = s;
super.setText(placeholder);
}
public String getPlaceholder()
{
return placeholder;
}
/*public boolean isPassword()
{
return isPassword;
}*/
/*public void setIsPassword(boolean b)
{
isPassword = b;
}*/
/*public String getText()
{
return text;
}*/
/*public void setText(String s)
{
if(isPassword && !isDefault)
{
text = s;
s = "";
for(int i = 0; i < text.length(); i++)
{
s += "*";
}
}
super.setText(s);
}*/
public void setText(String s)
{
super.setText(s);
isDefault = false;
}
public void clear()
{
setText("");
isDefault = true;
}
@Override
public void focusGained(FocusEvent arg0)
{
if(isDefault)
{
this.setText("");
isDefault = false;
}
}
@Override
public void focusLost(FocusEvent arg0)
{
if(getText().equals(""))
{
this.setText(placeholder);
isDefault = true;
}
else
{
isDefault = false;
}
}
}