Bom Dia,
Estou criando um JFormattedTextField especializado, mas está ocorrendo
um erro ao compilar, poderiam ajudar a identificar o que está errado?
Erro:
cannot find symbol
jcomp1 = new JFormatT1(new MaskFormatter(“AAAAA”));
location: class JFormatT1
symbol : constructor JFormatT1(javax.swing.text.MaskFormatter)
segue o código:
import javax.swing.*;
import java.awt.*;
import java.text.*;
import javax.swing.text.MaskFormatter;
public class JFormattedTextFieldNovo extends JPanel {
private JFormatT1 jcomp1;
private JFormattedTextField jcomp2;
private JFormattedTextField jcomp3;
private JFormattedTextField jcomp4;
public JFormattedTextFieldNovo() {
try {
// aceita caracter ou número.
jcomp1 = new JFormatT1(new MaskFormatter("AAAAA"));
// aceita somente números.
jcomp2 = new JFormattedTextField(new MaskFormatter("#####"));
// com máscara de data
jcomp3 = new JFormattedTextField(new MaskFormatter("##/##/####"));
// com máscara para valores financeiros.
jcomp4 = new JFormattedTextField(new DecimalFormat("#,###,##0.00"));
} catch (Exception ex) {
ex.printStackTrace();
}
setPreferredSize (new Dimension (384, 259));
setLayout (null);
add (jcomp1);
add (jcomp2);
add (jcomp3);
add (jcomp4);
jcomp1.setBounds (70, 60, 235, 25);
jcomp2.setBounds (70, 105, 235, 25);
jcomp3.setBounds (70, 150, 235, 25);
jcomp4.setBounds (70, 195, 235, 25);
}
public static void main (String[] args) {
JFrame frame = new JFrame ("JFormattedTextFieldNovo");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add (new JFormattedTextFieldNovo());
frame.pack();
frame.setVisible (true);
}
}
import java.awt.*;
import javax.swing.*;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.MaskFormatter;
import javax.swing.text.Document;
import javax.swing.text.PlainDocument;
public class JFormatT1 extends JFormattedTextField {
public JFormatT1() {
super();
this.setFont( new Font( "TAHOMA", Font.BOLD, 12));
this.setBorder(BorderFactory.createLineBorder(Color.black, 2));
}
protected Document createDefaultModel() {
return new UpperCaseDocument();
}
//Metodo que transforma todos os caracteres para Uppercase
static class UpperCaseDocument extends PlainDocument {
private static final long serialVersionUID = 1L;
public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {
if (str == null) { return; }
char[] upper = str.toCharArray();
for (int i = 0; i < upper.length; i++) {
upper[i] = Character.toUpperCase(upper[i]);
}
super.insertString(offs, new String(upper), a);
}
}
}