-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyTextBox.java
More file actions
53 lines (49 loc) · 1.27 KB
/
MyTextBox.java
File metadata and controls
53 lines (49 loc) · 1.27 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
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
/** Class extending JTextField. Sets up text box with hint.
*
* @author Gemma Smith <gemma.smith@tufts.edu>
* @version 1.0
* @since 2013-10-25
*/
public class MyTextBox extends JTextField implements FocusListener
{
private String prompt;
/** Standard constructor. Initializes text box with hint in light gray.
* Adds focus listener to clear the hint when user tries to type.
* @param p Prompt (hint)
*/
public MyTextBox (String p)
{
super(p, 10);
prompt = p;
addFocusListener(this);
setForeground(Color.LIGHT_GRAY);
}
/** Focus listener: when user tries to type, hint clears away.
*/
public void focusGained(FocusEvent e)
{
if (getText().equals(prompt))
{
setText("");
setForeground(Color.BLACK);
repaint();
revalidate();
}
}
/** Focus listener: when user leaves textfield blank, hint is restored.
*/
public void focusLost(FocusEvent e)
{
if (getText().equals(""))
{
setText(prompt);
setForeground(Color.LIGHT_GRAY);
repaint();
revalidate();
}
}
}