-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathField.java
More file actions
executable file
·65 lines (59 loc) · 2.07 KB
/
Field.java
File metadata and controls
executable file
·65 lines (59 loc) · 2.07 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
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.JTextField;
import java.awt.Font;
import java.awt.Color;
import java.io.File;
import java.awt.GraphicsEnvironment;
@SuppressWarnings("serial")
public class Field extends JTextField {
private String placeholder;
int limit;
public Field (String placeholder, int limit, boolean editable) {
this.placeholder = placeholder;
this.limit = limit;
/*
try {
GraphicsEnvironment ge =
GraphicsEnvironment.getLocalGraphicsEnvironment();
ge.registerFont(Font.createFont(Font.TRUETYPE_FONT, new File("font.ttf")));
} catch (Exception e) {
//Handle exception
}
setFont(new Font("Futura Book", Font.PLAIN, 18));*/
setFont(new Font("Arial", Font.PLAIN, 18));
setForeground(Color.decode("#333333"));
setDocument(new JTextFieldLimit(limit));
setEditable(editable);
}
public Field (int limit) {
this.placeholder = "";
this.limit = limit;
/*
try {
GraphicsEnvironment ge =
GraphicsEnvironment.getLocalGraphicsEnvironment();
Font font = Font.createFont(Font.TRUETYPE_FONT, new File("font.ttf"));
font = font.deriveFont(Font.BOLD,18);
ge.registerFont(font);
setFont(font);
} catch (Exception e) {
//Handle exception
}*/
setFont(new Font("Arial", Font.PLAIN, 18));
setForeground(Color.decode("#333333"));
setDocument(new JTextFieldLimit(limit));
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
if (super.getText().length() > 0 || placeholder == null) {
return;
}
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setColor(super.getDisabledTextColor());
g2.drawString(placeholder, getInsets().left, g.getFontMetrics().getMaxAscent() + getInsets().top);
}
}