Package practica1.util

Source Code of practica1.util.JTextFieldNumerico

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package practica1.util;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JTextField;

/**
* Componente que extiende de JTextField y permite solo escribir números
* @author Miguel González - Ceura
*/
public class JTextFieldNumerico extends JTextField {
   
    /**
     * Constructor por defecto
     */
    public JTextFieldNumerico() {
        addKeyListener(new KeyListener() {
            @Override
            public void keyTyped(KeyEvent e) {
                char c = e.getKeyChar();

                //Si no es un número y no es delete o backspace consumimos el evento
                if (!(Character.isDigit(c) ||
                    (c == KeyEvent.VK_BACK_SPACE) ||
                        (c == KeyEvent.VK_DELETE))) {
                    e.consume();
                }
            }

            @Override
            public void keyPressed(KeyEvent e) {}

            @Override
            public void keyReleased(KeyEvent e) {}
           
        });
    }
}
TOP

Related Classes of practica1.util.JTextFieldNumerico

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.