2
I have a field JTextField
which must receive a monetary value from the user. I would like to add a mask to this field so that it format the inserted values as follows: 9.999,99.
2
I have a field JTextField
which must receive a monetary value from the user. I would like to add a mask to this field so that it format the inserted values as follows: 9.999,99.
5
The easiest way, @Rodox, is to use the class MaskFormatter
.
First you create a class instance MaskFormatter
with a constructor in this format:
MaskFormatter mascaraCampo = null;
try {
mascaraCampo= new MaskFormatter("#.###,##");
}catch (ParseException e) {
}
After that, you just need to instantiate the JFormattedTextField
, passing the mask as a parameter.
JFormattedTextField campoFormatado = new JFormattedTextField(mascaraCampo);
Functional Example:
public class Funcional {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setBounds(0, 0, 310, 330);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
MaskFormatter mascaraCampo = null;
try {
mascaraCampo = new MaskFormatter("#.###,##");
} catch (ParseException e) {
}
JFormattedTextField campoFormatado = new JFormattedTextField(mascaraCampo);
campoFormatado.setVisible(true);
campoFormatado.setBounds(25, 25, 245, 30);
frame.add(campoFormatado);
frame.setVisible(true);
}
}
Após isso, é só preciso instanciar o seu JTextField, como um JFormattedTextField.
, This is not possible because they are completely different components. Despite the second inheritance first, it is not possible to use one as if it were another.
The JTextField
was used to identify the JFormattedTextField
. Note the examples, at no time did I write a JTextField
receiving an instance of JFormattedTextField
.
instanciar o seu JTextField, como um JFormattedTextField
- this here can confuse novice users, because also gives to understand this, anyway, It was just an alert, if not find useful the tip, is right your.
I agree it gets ambiguous, but n was my first intention. I believe it is better structured now.
It sure looks better now :D
Browser other questions tagged java swing jtextfield
You are not signed in. Login or sign up in order to post.
Download this library, easy and fast;
http://tiagojavaprogramador.blogspot.com.br/p/minha-b.html
– user46036