Problemas con JFormattedTextField
Saludos compañeros, tengo un JFormattedTextField y solo quiero k puedan introducir caracteres numericos, y eso lo logro, xo el problema es que al poner la mascara lo limito a un numero exacto d digitos, y yo quiero que el usuario pueda introducir una cifra con uno, dos, tres o cuatro digitos, no limitarle a que introduzca cuatro digitos exactos por ejemplo. Me podriais ayudar por favor ??? Gracias por adelantado.
JFormattedTextField tft1 = new JFormattedTextField(NumberFormat.getIntegerInstance());
Tengo puesto este codigo:
JFormattedTextField p = new JFormattedTextField(NumberFormat.getIntegerInstance());
param.setBounds(10,10,100,100);
try
{
mascara=new MaskFormatter(" * ");
mascara.setValidCharacters("1234567890");
}
catch (ParseException e) {}
pero solo me imprime como textfield un cuadro muy pequeñito, como podria darle valores para hacerlo mas grande ??? el codigo de antes me valdria para meter cq valor numerico, sea d un digito, dos o tres digitos ???? gracias.
JFormattedTextField p = new JFormattedTextField(NumberFormat.getIntegerInstance());
param.setBounds(10,10,100,100);
try
{
mascara=new MaskFormatter(" * ");
mascara.setValidCharacters("1234567890");
}
catch (ParseException e) {}
pero solo me imprime como textfield un cuadro muy pequeñito, como podria darle valores para hacerlo mas grande ??? el codigo de antes me valdria para meter cq valor numerico, sea d un digito, dos o tres digitos ???? gracias.
Hola, acá te dejo con 3 formas distintas de hacer lo que querés. Me parece que la que mejor queda es la 3era. Saludos y suerte!
<code>
public class Pruebas extends JFrame {
private JFormattedTextField jtext1;
private JTextField jtext2;
private JTextField jtext3;
private MaskFormatter formatter;
Pruebas() {
Container pane = getContentPane();
pane.setLayout(new BorderLayout());
try {
formatter = new MaskFormatter("****");
formatter.setValidCharacters("0123456789");
formatter.setPlaceholderCharacter('0');
jtext1 = new JFormattedTextField(formatter);
pane.add(jtext1, BorderLayout.NORTH);
} catch (Exception e) {
System.err.println("Unable to add Phone");
}
jtext2 = new JFormattedTextField(NumberFormat.getIntegerInstance());
pane.add(jtext2, BorderLayout.CENTER);
jtext3 = new JTextField();
jtext3.setDocument(new TextFieldLimiter(4));
pane.add(jtext3, BorderLayout.SOUTH);
setSize(100,100);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
Pruebas frm = new Pruebas();
frm.setVisible(true);
}
}
class TextFieldLimiter extends PlainDocument {
int maxChar;
public TextFieldLimiter(int len) {
maxChar = len;
}
public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {
if ((getLength() + str.length()) <= maxChar) {
char [] chars = str.toCharArray();
for (Character c : chars) {
if (! Character.isDigit(c)) {
Toolkit.getDefaultToolkit().beep();
return;
}
}
super.insertString(offs, str, a);
} else
throw new BadLocationException("Insertion exceeds max size of document", offs);
}
}
</code>
<code>
public class Pruebas extends JFrame {
private JFormattedTextField jtext1;
private JTextField jtext2;
private JTextField jtext3;
private MaskFormatter formatter;
Pruebas() {
Container pane = getContentPane();
pane.setLayout(new BorderLayout());
try {
formatter = new MaskFormatter("****");
formatter.setValidCharacters("0123456789");
formatter.setPlaceholderCharacter('0');
jtext1 = new JFormattedTextField(formatter);
pane.add(jtext1, BorderLayout.NORTH);
} catch (Exception e) {
System.err.println("Unable to add Phone");
}
jtext2 = new JFormattedTextField(NumberFormat.getIntegerInstance());
pane.add(jtext2, BorderLayout.CENTER);
jtext3 = new JTextField();
jtext3.setDocument(new TextFieldLimiter(4));
pane.add(jtext3, BorderLayout.SOUTH);
setSize(100,100);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
Pruebas frm = new Pruebas();
frm.setVisible(true);
}
}
class TextFieldLimiter extends PlainDocument {
int maxChar;
public TextFieldLimiter(int len) {
maxChar = len;
}
public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {
if ((getLength() + str.length()) <= maxChar) {
char [] chars = str.toCharArray();
for (Character c : chars) {
if (! Character.isDigit(c)) {
Toolkit.getDefaultToolkit().beep();
return;
}
}
super.insertString(offs, str, a);
} else
throw new BadLocationException("Insertion exceeds max size of document", offs);
}
}
</code>
