Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Option 1) change your JTextField with a JFormattedTextField, like this:
- try {
- MaskFormatter mascara = new MaskFormatter("##.##");
- JFormattedTextField textField = new JFormattedTextField(mascara);
- textField.setValue(new Float("12.34"));
- } catch (Exception e) {
- ...
- }
- //Option 2) capture user's input from keyboard, like this:
- JTextField textField = new JTextField(10);
- textField.addKeyListener(new KeyAdapter() {
- public void keyTyped(KeyEvent e) {
- char c = e.getKeyChar();
- if ( ((c < '0') || (c > '9')) && (c != KeyEvent.VK_BACK_SPACE)) {
- e.consume(); // ignore event
- }
- }
- });
Add Comment
Please, Sign In to add comment