-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCampoDato.java
92 lines (77 loc) · 2.66 KB
/
CampoDato.java
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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Bayer
*/
//import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class CampoDato extends JTextField implements KeyListener {
private char tipo;
private int longitud;
private String dato;
public CampoDato() {
this.addKeyListener(this);
}
public CampoDato(String dato) {
this.dato=dato;
this.addKeyListener(this);
setText(dato);
}
public void setTipo(char tipo) {
this.tipo = tipo;
}
public void setLongitud(int longitud) {
this.longitud = longitud;
this.setColumns(longitud);//explica cuantas columnas estan escitas
}
@Override
public void keyTyped(KeyEvent e) {
char k = e.getKeyChar();
switch (Character.toUpperCase(tipo)) { //para omitir entre mayusculas y minusculas
case 'T':
if (!Character.isLetter(k) && k != ' ') {
e.consume();
} else {
if ((this.getText().length() > longitud)) {
e.consume();
}
}
break;
case 'E':
if ((!(Character.isDigit(k) || k == KeyEvent.VK_BACK_SPACE))) {
e.consume();
} else {
if (this.getText().length() > longitud) {
e.consume();
}
}
break;
case 'D':
if (!(Character.isDigit(k) || k == '.' || k=='-') ) {
e.consume();
}
if(k=='.' && getText().contains(".")){
e.consume();
}else {
if (this.getText().length() > longitud) {
e.consume();
}
}
}
}
@Override
public void keyPressed(KeyEvent e) {
//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void keyReleased(KeyEvent e) {
// throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}